Deskripsi
Kita akan membuat aplikasi (Client) yang bisa melakukan request ke Web Service (API)
menggunakan PHP Lumen. Tujuan praktek ini adalah untuk:
1. Memahami bagaimana mengakses cara Web Service (API) menggunakan PHP
2. Mampu implementasi membuat aplikasi (Client) yang bisa melakukan request ke Web
Service (API)
Sebelumnya kita membuat aplikasi Web Service menggunakan Lumen. Saat ini kita akan
membuat aplikasi yang bisa mengakses Web Service menggunakan Lumen (kita sebut dengan
aplikasi Client).
Tantangan
Dibawah ini adalah tantangan untuk kita:
1. Implementasi CRUD akses Rest API menggunakan Dummy API
http://dummy.restapiexample.com/
1. Buat Dummy Controller
Endpoint Get All Employee
public function getEmployee(){
Endpoint GetEmployeeById
Enpoint Delete Employee
Kita akan membuat aplikasi (Client) yang bisa melakukan request ke Web Service (API)
menggunakan PHP Lumen. Tujuan praktek ini adalah untuk:
1. Memahami bagaimana mengakses cara Web Service (API) menggunakan PHP
2. Mampu implementasi membuat aplikasi (Client) yang bisa melakukan request ke Web
Service (API)
Sebelumnya kita membuat aplikasi Web Service menggunakan Lumen. Saat ini kita akan
membuat aplikasi yang bisa mengakses Web Service menggunakan Lumen (kita sebut dengan
aplikasi Client).
Tantangan
Dibawah ini adalah tantangan untuk kita:
1. Implementasi CRUD akses Rest API menggunakan Dummy API
http://dummy.restapiexample.com/
1. Buat Dummy Controller
Endpoint Get All Employee
public function getEmployee(){
$url = 'http://dummy.restapiexample.com/api/v1/employees';
$headers = ['Accept:application/json'];
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($this->curl);
curl_close($this->curl);
$response = json_decode($result, true);
return response()->json($response,200);
}
Endpoint GetEmployeeById
public function getEmployeeById($id){
$url = 'http://dummy.restapiexample.com/api/v1/employee/'.$id;
$headers = ['Accept:application/json'];
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($this->curl);
curl_close($this->curl);
$response = json_decode($result, true);
return response()->json($response,200);
}
Enpoint PostEmployee
public function postEmployee(Request $request)
{
$url = 'http://dummy.restapiexample.com/api/v1/create';
$headers = ['Accept:application/json', 'Content-Type:application/json'];
$data = array(
'name' =>$request->input('name'),
'salary' =>$request->input('salary'),
'age' => $request->input('age'),
);
$dataJson = json_encode($data);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $dataJson);
$result = curl_exec($this->curl);
curl_close($this->curl);
$response = json_decode($result, true);
return response()->json($response,200);
}
Enpoint Update Employee
public function updateEmployee($id,Request $request)
{
$url = 'http://dummy.restapiexample.com/api/v1/update/'.$id;
$headers = ['Accept:application/json', 'Content-Type:application/json'];
$data = array(
'name' =>$request->input('name'),
'salary' =>$request->input('salary'),
'age' => $request->input('age'),
);
$dataJson = json_encode($data);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $dataJson);
$result = curl_exec($this->curl);
curl_close($this->curl);
$response = json_decode($result, true);
return response()->json($response,200);
}
Enpoint Delete Employee
public function deleteEmployeeById($id){
$url = 'http://dummy.restapiexample.com/api/v1/delete/'.$id;
$headers = ['Accept:application/json'];
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST,"DELETE");
$result = curl_exec($this->curl);
curl_close($this->curl);
$response = json_decode($result, true);
return response()->json($response,200);
}
Selanjutnya masukan route nya
//DummyRestApi
$router->get('/dummy/get-employees','DummyRestController@getEmployee');
$router->get('/dummy/get-employee/{id}','DummyRestController@getEmployeeById');
$router->post('/dummy/create','DummyRestController@postEmployee');
$router->put('/dummy/update/{id}','DummyRestController@updateEmployee');
$router->delete('/dummy/delete/{id}','DummyRestController@deleteEmployeeById');
Selanjutnya testing enpoint nya jalankan aplikasinya dengan
ketik di console php -S localhost:9000 -t public
Untuk endpoint get all employee
Hit enpoint localhost:9000/dummy/get-employees
Untuk endpoint get employee by id
Hit enpoint localhost:9000/dummy/get-employee/1
Untuk endpoint post employee
Hit enpoint localhost:9000/dummy/create
Sekian tutorial kali ini, Terimakasih
Labels:
PHP
0 Komentar untuk "PHP Lumen Client App"