Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Guzzle send async web requests?

Tags:

php

guzzle

cURL is synchronous. So how do libraries like Guzzle send asynchronous web requests?

like image 380
Code Avatar asked Feb 26 '16 15:02

Code


People also ask

Is Guzzle asynchronous?

Guzzle allows you to send both asynchronous and synchronous requests using the same interface and no direct dependency on an event loop. This flexibility allows Guzzle to send an HTTP request using the most appropriate HTTP handler based on the request being sent.

How do you send asynchronous request?

You can also send requests synchronously by calling WdfRequestSend, but you have to format the request first by following the rules that are described in Sending I/O Requests Asynchronously. Sending I/O requests to an I/O target synchronously is simpler to program than sending I/O requests asynchronously.

How does async request work?

Asynchronous request. If you use an asynchronous XMLHttpRequest , you receive a callback when the data has been received. This lets the browser continue to work as normal while your request is being handled.

How do I send a POST request with Guzzle?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);


2 Answers

One of the Guzzle's transport handlers is CurlMultiHandler that uses PHP's curl_multi_* functions which allows for asynchronous transfers.

The requests are launched asynchronously and the function curl_multi_select() allows Guzzle to wait until one of the curl requests receives data and process it.

like image 95
axiac Avatar answered Oct 14 '22 01:10

axiac


The Guzzle CurlMultiHander wraps PHP's builtin curl_multi_* function which essentially wrap the cURL Multi API

From the cURL documents:

To use the multi interface, you must first create a 'multi handle' with curl_multi_init. This handle is then used as input to all further curl_multi_* functions.

With a multi handle and the multi interface you can do several simultaneous transfers in parallel. Each single transfer is built up around an easy handle. You create all the easy handles you need, and setup the appropriate options for each easy handle using curl_easy_setopt.

like image 4
Shaun Bramley Avatar answered Oct 14 '22 01:10

Shaun Bramley