[The PHP] transmitted using an asynchronous HTTP request guzzle

PHP request a HTTP service, are generally blocked, if there are multiple HTTP requests need the serial execution, a request by one, you can use this library to guzzle php simultaneously send multiple asynchronous HTTP requests.

The response time depends on the results of the longest response time to that request

 

Test code is as follows:

                $client = new \GuzzleHttp\Client();
                // 发送一个异步请求
                $request = new \GuzzleHttp\Psr7\Request('GET', 'http://www.sopans.com');
                $request2 = new \GuzzleHttp\Psr7\Request('GET', 'http://www.sopans.com/laruence');
                $request3 = new \GuzzleHttp\Psr7\Request('GET', 'http://www.sopans.com/about');
                $promise = $client->sendAsync($request)->then(function ($response) {
                                echo 111;
                                }); 
                $promise = $client->sendAsync($request2)->then(function ($response)use($client,$request3) {
                                $client->sendAsync($request3)->then(function ($res){
                                                echo 444;
                                                }); 
                                echo 222;
                                }); 
                echo 333;
                $promise->wait();

Back 333 111 444 222, the following code is executed first, in response to the request after performing the local code to achieve the effect asynchronous

Installation composer

composer require guzzlehttp/guzzle

  

Guess you like

Origin www.cnblogs.com/taoshihan/p/12668957.html
Recommended