guzzle6 实现多线程请求

2019-02-27  本文已影响0人  你的坚持终将美好_zxz

public function info()

{

 $client = new Client();

 $requests = function ($total) {

     $baseUri = 'https://www.zlsuper.top';

     for ($i = 0; $i < $total; $i++) {

         yield new Request('GET', $baseUri);

     }

 };

 $ret = [];

 $pool = new Pool($client, $requests(10), [

     'concurrency' => 5,

     'fulfilled' => function (Response $response, $index) use (&$ret) {

         // this is delivered each successful response

         $contents = $response->getBody()->getContents();

         $contents = json_decode($contents, true);

         $ret[$index] = $contents;

     },

     'rejected' => function (RequestException $reason, $index) {

         // this is delivered each failed request

     },

 ]);

 // Initiate the transfers and create a promise

 $promise = $pool->promise();

 // Force the pool of requests to complete.

 $promise->wait();

 echo json_encode($ret);

 die;

}

上一篇下一篇

猜你喜欢

热点阅读