Guzzle
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
Guzzle 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 but 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 'Response is completed! ';
echo 'Response: ' . json_decode($response->getBody())->{'full_name'};
}
);
$promise->wait();
// Response is completed!
// Response: guzzle/guzzle
Last update: 340 days ago