美文网首页
guzzle6 实现多线程请求

guzzle6 实现多线程请求

作者: 你的坚持终将美好_zxz | 来源:发表于2019-02-27 09:58 被阅读0次
    
    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;
    
    }
    
    

    相关文章

      网友评论

          本文标题:guzzle6 实现多线程请求

          本文链接:https://www.haomeiwen.com/subject/fllxuqtx.html