0% found this document useful (0 votes)
17 views31 pages

Sister.2019 14 Pemrograman Restful API Ws

Uploaded by

thex thex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views31 pages

Sister.2019 14 Pemrograman Restful API Ws

Uploaded by

thex thex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Membangun Web Service

dengan PHP
Husni
Framework Pengembangan Web Service
• SOAP:
• WSO2 Web Service Framework for PHP (WSF/PHP)
• NuSOAP

• REST:
• No Framework
• Laravel
• CodeIgniter
• Flight PHP
• Slim
Contoh SOAP: Server
Contoh SOAP: Client
• Client menjalankan perintah atau fungsi yang terdapat di server
• Mirip dengan Remote Procedure Call (RPC)
SOAP vs. REST
Web Service Sederhana: index.php
Web Service Sederhana: connectdb.php

Database: db_ws
Tabel: tb_01
Mengakses Web Service: readws.php
Mengakses Web Service dari Android:
UserFunctions.java
Contoh Server REST: ws01.php
Contoh Client REST: client01.php
Integrasi 2 Aplikasi
• Aplikasi A (di mesin 192.168.1.1) menyimpan data login setiap
pengguna, data ini boleh dimanfaatkan oleh aplikasi lain yang
diijinkan.
• Aplikasi B (di mesin 192.168.2.2) menyediakan halaman login untuk
penggunanya. Aplikasi ini memeriksa data pengguna di Server A (data
pengguna tidak dipegang oleh Aplikasi B)
• Pendekatan ini yang sedang trend saat ini. Akun google, twitter dan
facebook dapat digunakan untuk login ke berbagai layanan yang
berjalan di Internet.
Data di Aplikasi A (Mesin 192.168.1.1)
Layanan Pengecekan Username & Password
WSCekpassword.php
Aplikasi B
• Berjalan pad amesin 192.168.2.2
• Menyediakan layanan login, cek di aplikasi B, jika username &
password cocok → login berhasil.
Aplikasi B: Login.php
Aplikasi A Cek Kode API Consumer
Aplikasi B mendapatkan Kode API “1234”
Contoh Login Antar Server (lagi)
Server B menyediakan layanan login, data user di server A
Database db_ws01
• Tabel tb_user
• CREATE TABLE user (
email varchar(100) NOT NULL,
name varchar(100),
password varchar(200),
PRIMARY KEY (email)
)
• 2 record data awal:
• INSERT INTO tb_user (email, name, password)
VALUES (‘[email protected]', ‘Husni Ilyas', '21232f297a57a5a743894a0e4a801fc3');
INSERT INTO tb_user (email, name, password)
• VALUES (‘[email protected]', 'Azzam Altaf', ' fe01ce2a7fbac8fafaed7c982a04e229');

• 21232f297a57a5a743894a0e4a801fc3 = admin
• fe01ce2a7fbac8fafaed7c982a04e229 = demo
Web Service api.php di Server A
1. <?php
2. //Koneksi ke database
3. mysql_connect("localhost", "root", "") or die(mysql_error());
4. mysql_select_db("test") or die(mysql_error());
5.
6. //cek email & passwordnya
7. $email = $_POST['email'];
8. $password = $_POST['password'];
9.
10. $Q = mysql_query("SELECT * FROM user WHERE email='$email'
11. AND PASSWORD='$password'") or die(mysql_error());
12. if($Q){
13. $posts = array();
14. if(mysql_num_rows($Q)) {
15. while($post = mysql_fetch_assoc($Q)){ $posts[] = $post; }
16. }
17. echo json_encode(array('user'=>$posts));
18. }
19. ?>
Server A: Formlogin.php
1. <form class="form-signin" action="" method="post">
2. <h2 class="form-signin-heading">Please sign in</h2>
3. <input type="text" class="input-block-level" name="email" placeholder="Email address">
4. <input type="password" class="input-block-level" name="password" placeholder="Password">
5. <button class="btn btn-large btn-primary" type="submit">Sign in</button>
6. </form>
Server B: Login.php
1. <?php
2. function doLogin($url, $email, $password){
3.
4. $datauser = array(
5. //'API_key' => $key,
6. 'email' => $email,
7. 'password' => $password,
8. );
9.
10. $postdatauser = "";
11. foreach($datauser as $k => $v) {
12. $postdatauser .= $k . "=" . $v."&";
13. }
14. //$postData = http_build_query($user_data);
15. $curlHandle = curl_init();
16. curl_setopt($curlHandle, CURLOPT_URL, $url);
17. curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postdatauser); //
18. curl_setopt($curlHandle, CURLOPT_HEADER, 0);
Server B: Login.php
19. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
20. curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
21. curl_setopt($curlHandle, CURLOPT_POST, 1);
22. $string = curl_exec($curlHandle);
23. curl_close($curlHandle);
24.
25. return $string;
27. }

27. //cara login


28. echo doLogin("http://Server_A/login/api.php", $_POST[“email”],
$_POST[“password”]);
Server B: Login.php
29. //proses jSON yang dikembalikan Server A
30. //$string = doLogin("http://localhost/public/login/api.php", $username, $password);
31. $arr = json_decode($string,true);
32. $email = $arr['user'][0]['email'];
33. $password = $arr['user'][0]['password'];
34. //cek keberhasilan login
35. if($email !=''){
36. session_start();
37. $_SESSION['email'] = $email;
38. $_SESSION['password'] = $password;
39. header("Location:suksess.php"); //arahkan ke halaman suksess
40. } else { echo "Authentication Failed!"; }
Membuat Aplikasi Pelanggan RESTful
Sederhana dengan PHP + curl
Contoh: Mengirimkan Permintaan GET
$service_url = 'http://localhost/wslim/api/todo/5';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Contoh: Mengirimkan Permintaan POST
$service_url = 'http://localhost/wslim/api/todo';
$curl = curl_init($service_url);
$curl_post_data = array(
‘task' => 'testing modul baru',
‘status' => ‘0',
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
Contoh: Memanggil Permintaan POST
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR')
{
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Contoh: Mengirimkan Permintaan PUT
$service_url = 'http://localhost/wslim/api/todo/3';
$ch = curl_init($service_url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");


$data = array("status" => ‘1');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
if ($response === false) {
$info = curl_getinfo($ch); curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($ch);
$decoded = json_decode($response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);
Contoh: Mengirimkan Permintaan DELETE
$service_url = 'http://localhost/wslim/api/todo/4';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$response = curl_exec($ch);
if ($curl_response === false) {
$info = curl_getinfo($curl); curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
echo 'response ok!';
var_export($decoded->response);

You might also like