PHP LUMEN CLIENT APP

 PHP LUMEN CLIENT APP

1.      Membuat Aplikasi Lumen

Pertama buat dulu Lumen Clientnya, caranya sama seperti tutorial sebelumnya tentang instalasi lumen. Setelah aplikasi sudah terinstall, buka foldernya lalu masuk ke routes/web.php, kemudian tambahkan routes seperti dibawah ini:

$router->get('/posts/get-request-json''PostsController@getRequestJson');

$router->get('/posts/post-request-json''PostsController@postRequestJson');

 

2.      Membuat Fungsi Untuk Mengakses Web Service

1)       Buat controller dengan nama app/http/controllers/PostsController.php dan tuliskan coding seperti dibawah ini :

public function getRequestJson(Request $req)

    {

        $url = 'http://localhost:8000/public/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);

        $err = curl_error($ch);

 

        curl_close($ch);

 

        $response = json_decode($resulttrue);

        // return $response;

 

        return view('posts/getRequestJson', ['results' => $response]);

    }

 

    public function postRequestJson(Request $req)

    {

        $url = 'http://localhost:8000/public/posts';

        $headers = ['Accept: aplication/json''Content-Type: aplication/json'];

        $data = array(

            "title" => "ini adalah title",

            "content" => "ini adalah content",

            "status" => "published",

            "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($resulttrue);

 

        return view('posts/postRequestJson', ['result' => $response]);

    }

 

2)       Setelah itu buat menu view, disini saya menggunakan bootstrap untuk styling, buat file view dengan nama resource/views/posts/getRequestJson.php dan tuliskan coding seperti ini:

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

 

</head>

<body>

    <div class="container">

        <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>Update At</th>

                </tr>

            </thead>

            <tbody>

                <?php

                    foreach($results['data'] 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>

 

3)       Buat file view untuk posts-request json, resource/views/posts/postRequestJson.php kemudian coding code seperti dibawah ini.

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

 

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">

 

</head>

<body>

    <div class="container">

        <h1>List Post</h1>

        <table class="table table-striped">

            <tbody>

                <?php foreach($result['data'] as $result) ?>

                <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>Content</td>

                    <td><?php echo $result['content'] ?></td>

                </tr>

                <tr>

                    <td>Status</td>

                    <td><?php echo $result['status'] ?></td>

                </tr>

                <tr>

                    <td>Created At</td>

                    <td><?php echo $result['created_at'] ?></td>

                </tr>

                <tr>

                    <td>Update At</td>

                    <td><?php echo $result['updated_at'] ?></td>

                </tr>

                <?php  ?>

            </tbody>

        </table>

    </div>

</body>

</html>

4)       Kemudian Test, jalankan Web Service dengan : php -S localhost:8000 -t public dan Client App dengan : php -S localhost:9000 -t public

5)       Dan hasilnya akan seperti berikut :

a.       Tampilan get-request-json

b.      Tampilan post-request-json

 

Komentar

Postingan populer dari blog ini

KONSEP, KELEBIHAN, KEKURANGAN CONTAINERIZATION BERSERTA TEKNOLOGI DAN STUDI KASUS CONTAINERIZATION

Mengapa Harus Belajar Cloud Computing ?

Setting Up VM and Domain