rinat.io


Guzzle

I never liked to use php curl functions. Consider this code which sends a simple POST request:

$resource = curl_init('http://example.com');
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, ['name' => 'value']);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($resource);
curl_close($resource);

It is so unreadable and hard! And all those curl_setopt constants...

But recently I discovered an amazing library with funny name: Guzzle. It is so powerful and simple, I'd like to use it everywhere. Here's a code for the same POST request with Guzzle:

Guzzle\Http\StaticClient::mount();
$response = Guzzle::post('http://example.com', [
    'body' => ['name' => 'value']
])->getMessage();

Isn't it simple?

If you look at the documentation you will find how flexible and powerful it is. It even allows to send a parallel requests through curl_multi_* functions. Guzzle is used by Drupal and other php libraries like Amazon AWS SDK and Goutte. Alright, try it yourself!