PROGRAMMING

  MINTE9
REMEMBERS




Last update:   28-03-2022

Guzzle

You can add Guzzle as a dependency using Composer.
 
/** 
 * Guzzle is a HTTP client library for PHP, initially a wrapper
 * library around cURL. It evolved to a transport agnostic 
 * PSR-7 compatible library.
 * 
 * composer require guzzlehttp/guzzle
 */

require __DIR__ . '/vendor/autoload.php';

$client = new GuzzleHttp\Client();
$response = $client->request('GET', 'http://httpbin.org/get');
$json = json_decode($response->getBody());

echo $json->origin; // 188.27.97.4

Api

The main benefits of using Guzzle over cURL is the API it offers.
 
/**
 * Guzzle is an abstraction layer for http request.
 * It uses cURL by default you can use any other http client that you want.
 * 
 * Send an asynchronous request:
 */

require __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$client = new Client();
$request = new Request('GET', 'https://api.github.com/repos/guzzle/guzzle');

$promise = $client->sendAsync($request)->then(
    function ($response) {
        echo 'I completed! ' . $response->getBody();
    }
);
$promise->wait();

Questions     github Composer
Carbon
        A B C D E F
🔔
1/1