Tutorial PHP Lumen Client App

Dalam tutorial kali ini, kita akan membuat aplikasi Client yang bisa melakukan request ke Web Service (API) menggunakan PHP Lumen.

Membuat Project Baru


Kita harus membuat projek Lumen baru terlebih dahulu

Pastikan PC/Laptop anda terkoneksi internet

Jalankan perintah dibawah ini pada command promt (terminal) :

composer create-project --prefer-dist laravel/lumen [NAMA_PROJECT]

dalam kasus ini kita beri nama projectnya LumenClientApp

composer create-project --prefer-dist laravel/lumen LumenClientApp

Selah itu tekan enter. Tunggu hingga proses selesai. Prosesnya akan mendownload modul-modul yang dibutuhkan untuk projek


Jika proses sudah selesai, masuk ke direktori folder projek yang telah diinstalasi/didownload dalam command promt :

cd LumenClientApp

Jalankan servernya di command promt. Kali ini kita akan menggunakan port 9000 :

php -S localhost:9000 - t public



Silahkan coba jalankan pada browser http://localhost:9000 untuk bukti bahwa Lumen berjalan




Membuat Fungsi Untuk Mengakses Web Service JSON (Get Request)


Untuk membuat fungsi yang bisa mengakses Web Service JSON, silahkan ikuti langkah-langkah dibawah ini:


Buka file routes/web.php, tambahkan code dibawah ini, line 18




Buat controller dengan nama app/Http/Controllers/PostsController.php, buat function getRequestJson dengan code nya seperti dibawah ini.


 <?php  
 namespace App\Http\Controllers;  
 use Illuminate\Http\Request;  
 class PostsController extends Controller{  
      public function getRequestJson(Request $request){  
           $url = 'http://localhost:8000/posts';  
           $headers = ['Accept:application/json'];  
           $ch = curl_init();  
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($ch, CURLOPT_URL, $url);  
           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
           $result=curl_exec($ch);  
           curl_close($ch);  
           $response = json_decode($result, true);  
           return view('posts/getRequestJson', ['results' => $response]);  
      }  
 }  
 ?>  

Kita akan menggunakan module view untuk menampilkan dalam bentuk html.

Sekarang buat file view dengan nama resources/views/posts/getRequestJson.php.
Untuk styling nya kita akan menggunakan bootstrap (https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css ). Codenya seperti dibawah ini.


 <html>  
 <head>  
 <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>  
 </head>  
 <body>  
 <div class="contrainer">  
 <h1>List Post</h1>  
 <table class="table table-striped">  
      <thead class="thead-dark">   
      <tr>  
           <th>ID</th>  
           <th>User ID</th>  
           <th>Title</th>  
           <th>Status</th>  
           <th>Content</th>  
           <th>Created At</th>  
           <th>Updated At</th>  
           </tr>  
      </thead>  
      <tbody>  
           <?php  
                foreach($results as $result) {  
                     echo "<tr>";  
                     echo "<td>".$result['id']."</td>";  
                     echo "<td>".$result['user_id']."</td>";  
                     echo "<td>".$result['title']."</td>";  
                     echo "<td>".$result['status']."</td>";  
                     echo "<td>".$result['content']."</td>";  
                     echo "<td>".$result['created_at']."</td>";  
                     echo "<td>".$result['updated_at']."</td>";  
                     echo "</tr>";  
                }  
           ?>  
      </tbody>  
 </table>  
 </div>  
 </body>  
 </html>  

Buka halaman http://localhost:9000/posts/get-request-json di browser, tampilanya
seperti dibawah ini:


Membuat Fungsi Untuk Mengakses Web Service XML (Get Request)

Buka file routes/web.php, tambahkan code dibawah ini, line 19


Buat controller dengan nama app/Http/Controllers/PostsController.php, buat function
getRequestXml dengan code nya seperti dibawah ini. Tambahkan di bawa class getRequestJson

 public function getRequestXml(Request $request){  
           $url = 'http://localhost:8000/posts';  
           $headers = ['Accept: application/xml'];  
           $ch = curl_init();  
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($ch, CURLOPT_URL, $url);  
           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
           $result=curl_exec($ch);  
           curl_close($ch);  
           $parsedXml = new \SimpleXMLElement($result);  
           $response = [];  
           foreach($parsedXml->children() as $item) {  
                array_push($response, array(  
                     'id' => $item->id,  
                     'user_id' => $item->user_id,  
                     'title' => $item->title,  
                     'status' => $item->status,  
                     'content' => $item->content,  
                     'created_at' => $item->created_at,  
                     'updated_at' => $item->updated_at,  
                ));  
           }  
           return view('posts/getRequestXml', ['results' => $response]);  
      }  

Sekarang buat file view dengan nama resources/views/posts/getRequestXml.php.
Untuk styling nya kita akan menggunakan bootstrap (https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css ). Codenya seperti dibawah ini.

 <html>  
 <head>  
 <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>  
 </head>  
 <body>  
 <div class="contrainer">  
 <h1>List Post</h1>  
 <table class="table table-striped">  
      <thead class="thead-dark">   
      <tr>  
           <th>ID</th>  
           <th>User ID</th>  
           <th>Title</th>  
           <th>Status</th>  
           <th>Content</th>  
           <th>Created At</th>  
           <th>Updated At</th>  
           </tr>  
      </thead>  
      <tbody>  
           <?php  
                foreach($results as $result) {  
                     echo "<tr>";  
                     echo "<td>".$result['id']."</td>";  
                     echo "<td>".$result['user_id']."</td>";  
                     echo "<td>".$result['title']."</td>";  
                     echo "<td>".$result['status']."</td>";  
                     echo "<td>".$result['content']."</td>";  
                     echo "<td>".$result['created_at']."</td>";  
                     echo "<td>".$result['updated_at']."</td>";  
                     echo "</tr>";  
                }  
           ?>  
      </tbody>  
 </table>  
 </div>  
 </body>  
 </html>  

Buka halaman http://localhost:9000/posts/get-request-xml di browser, tampilanya seperti
dibawah ini:



Membuat Fungsi Untuk Mengakses Web Service JSON (Post Request)

Buka file routes/web.php, tambahkan code dibawah ini, line 20


Buka controller app/Http/Controllers/PostsController.php, buat function postRequestJson dengan code nya seperti dibawah ini. Tambahkan dibawa function getRequestXml

 public function postRequestJson(Request $request){  
           $url = 'http://localhost:8000/posts';  
           $headers = ['Accept: application/json', 'Content-Type: application/json'];  
           $data = array(  
                "title" => "Ini adalah Judul",  
                "content" => "Ini adalah Konten",  
                "status" => "draft",  
                "user_id" => "1"  
           );  
           $dataJSON = json_encode($data);  
           $ch = curl_init();  
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
           curl_setopt($ch, CURLOPT_URL, $url);  
           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
           curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJSON);  
           $result=curl_exec($ch);  
           curl_close($ch);  
           $response = json_decode($result, true);  
           return view('posts/postRequestJson', ['result' => $response]);  
      }  

Sekarang buat file view dengan nama resources/views/posts/postRequestJson.php.
Untuk styling nya kita akan menggunakan bootstrap (https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css ). Codenya seperti dibawah ini.

 <html>  
 <head>  
 <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>  
 </head>  
 <body>  
 <div class="contrainer">  
 <h1>List Post</h1>  
 <table class="table table-striped">  
      <tbody>  
      <tr>  
           <td>ID</td>  
           <td><?php echo $result['id']?></td>  
      </tr>  
      <tr>  
           <td>User ID</td>  
           <td><?php echo $result['user_id']?></td>  
      </tr>  
      <tr>  
           <td>Title</td>  
           <td><?php echo $result['title']?></td>  
      </tr>  
      <tr>  
           <td>Status</td>  
           <td><?php echo $result['status']?></td>  
      </tr>  
      <tr>  
           <td>Content</td>  
           <td><?php echo $result['content']?></td>  
      </tr>  
      <tr>  
           <td>Created At</td>  
           <td><?php echo $result['created_at']?></td>  
      </tr>  
      <tr>  
           <td>Updated At</td>  
           <td><?php echo $result['updated_at']?></td>  
           </tr>  
      </tbody>  
 </table>  
 </div>  
 </body>  
 </html>  

Buka halaman http://localhost:9000/posts/post-request-json di browser, tampilanya seperti dibawah ini:


Selesai

Komentar

Postingan Populer