PHP Multi Curl Request,在等待响应时做一些工作

PHP Multi Curl Request,在等待响应时做一些工作

问题描述:

Here's a typical multi-curl request example for PHP:

$mh = curl_multi_init();

foreach ($urls as $index => $url) {
    $curly[$index] = curl_init($url);
    curl_setopt($curly[$index], CURLOPT_RETURNTRANSFER, 1);
    curl_multi_add_handle($mh, $curly[$index]);
}

// execute the handles
$running = null;
do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while($running > 0);

// get content and remove handles
$result = array();
foreach($curly as $index => $c) {
    $result[$index] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
}

// all done
curl_multi_close($this->mh);

The process involves 3 steps: 1. Preparing data 2. Sending requests and waiting until they're finished 3. Collecting responses

I'd like to split step #2 into 2 parts, first send all the requests and then collect all the response, and do some useful job instead of just waiting, for example, processing the responses of last group of requests.

So, how can I split this part of code

$running = null;
do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while($running > 0);

into separate parts?

  1. Send all the requests
  2. Do some another job while waiting
  3. Retrieve all the responses

I tried like this:

// Send the requests
$running = null;
curl_multi_exec($mh, $running);

// Do some job while waiting
// ...

// Get all the responses
do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while($running > 0);

but it doesn't seem to work properly.

以下是PHP的典型多卷曲请求示例: p>

  $ mh = curl_multi_init(); 
 
foreach($ urls as $ index => $ url){
 $ curly [$ index] = curl_init($ url); 
 curl_setopt($ curly [$ index]  ,CURLOPT_RETURNTRANSFER,1); 
 curl_multi_add_handle($ mh,$ curly [$ index]); 
} 
 
 //执行句柄
 $ running = null; 
do {
 curl_multi_exec($ mh,  $ running}; 
 curl_multi_select($ mh); 
} while($ running> 0); 
 
 //获取内容并删除句柄
 $ result = array(); 
foreach($ curly as  $ index => $ c){
 $ result [$ index] = curl_multi_getcontent($ c); 
 curl_multi_remove_handle($ mh,$ c); 
} 
 
 //所有已完成
curl_multi_close($  this-> mh); 
  code>  pre> 
 
 

该过程包括3个步骤: 1。 准备数据 2。 发送请求并等待它们完成 3。 收集回复 p>

我想将第2步分成两部分,首先发送所有请求然后收集所有响应,然后做一些有用的工作而不是等待,例如 处理最后一组请求的响应。 p>

那么,如何拆分这部分代码 p>

  $ running = null;  
do {
 curl_multi_exec($ mh,$ running); 
 curl_multi_select($ mh); 
} while($ running> 0); 
  code>  pre> 
 
 

分成不同的部分? p>

  1. 发送所有请求 li>
  2. 等待时再做一些工作 li>
  3. 检索 所有的回复 li> ol>

    我试过这样的: p>

      //发送请求
     $ running = null  ; 
    curl_multi_exec($ mh,$ running); 
     
     //在等待时做一些工作
     // ... 
     
     //获取所有响应
    do {
     curl_multi_exec($ mh,$ 运行); 
     curl_multi_select($ mh); 
    } while($ running> 0); 
      code>  pre> 
     
     

    但它似乎无法正常工作。 p> div>

I found the solution, here's how to launch all the requests without waiting the response

do {
    curl_multi_exec($mh, $running);
} while (curl_multi_select($mh) === -1);

then we can do any other jobs and catch the responses any time later.