Bagi developer PHP yang familiar dengan API, mungkin tidak asing lagi dengan Guzzle. Guzzle adalah http client yang bisa digunakan untuk mengirim dan mengambil data dari suatu server, bisa dibilang juga sebagai browsernya developer.

Banyak framework PHP yang menggunakan guzzle seperti Laravel, CodeIgniter tetapi jangan salah kaprah karena Guzzle tetap bisa digunakan di PHP. Disini contoh yang akan dibuat tanpa bergantung ke framework

Folder project yang saya gunakan disini adalah /home/tommy/git/restapi

Install Guzzle

Cara paling mudah menginstall Guzzle adalah dengan composer, karena dependencynya langsung diurus oleh composer tersebut. Pindah ke folder project anda lalu jalankan

composer require guzzlehttp/guzzle

Menggunakan Guzzle

Cara menggunakan guzzle setelah menginstall dengan composer, pindah ke folder project anda, buat file index.php (bisa dinamai apa pun), tambahkan

<?php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;

dengan dua baris code tersebut Guzzle sudah bisa mulai digunakan.

GET Method dengan Guzzle

Metode GET biasa digunakan untuk mengambil data dari server, sebagai contoh kita akan mengambil data user dari API reqres.in

<?php
// index.php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
 
$client = new Client([
    'base_uri' => 'https://reqres.in',
    // default timeout 5 detik
    'timeout'  => 5,
]);
 
$response = $client->request('GET', '/api/users');
$body = $response->getBody();
$body_array = json_decode($body);
print_r($body_array);

response get guzzle php

cara diatas, bisa dipersingkat menjadi

<?php
// index.php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
 
$client = new Client();
$response = $client->request('GET', 'https://reqres.in/api/users');
$body = $response->getBody();
$body_array = json_decode($body);
print_r($body_array);

tetapi cara kedua ini tidak direkomendasikan, karena code menjadi lebih sudah dibaca. Contoh dari response GET tersebut memiliki dua halaman, untuk mengambil data dari halaman kedua

<?php
// index.php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
 
$client = new Client([
    'base_uri' => 'https://reqres.in',
    // default timeout 5 detik
    'timeout'  => 5,
]);
 
$response = $client->request('GET', '/api/users', [
    'query' => [
        'page' => '2'
    ]
]);
$body = $response->getBody();
$body_array = json_decode($body);
print_r($body_array);

POST Method dengan Guzzle

Metode POST biasa digunakan untuk mengirim data ke server, contoh yang biasa digunakan dengan POST adalah mengirim data login, mengupload gambar dan lain sebagainya.

<?php
// index.php
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
 
$client = new Client([
    'base_uri' => 'https://reqres.in',
    // default timeout 5 detik
    'timeout'  => 5,
]);
 
$response = $client->request('POST', '/api/login', [
    'json' => [
        'email' => '[email protected]',
        'password' => 'cityslicka'
    ]
]);
$body = $response->getBody();
$body_array = json_decode($body);
print_r($body_array);

Join the Conversation

1 Comment

Your email address will not be published. Required fields are marked *