如何立即访问PHP多cURL处理响应

时间:2019-03-05 07:56:39

标签: php curl curl-multi

我正在请求具有不同响应时间的多个地址:

$urlList = [
    'https://httpstat.us/200?sleep=3000',
    'https://httpstat.us/200?sleep=1000'
];

$mh = curl_multi_init();

$curlHandlerList = [];
foreach ($urlList as $i => $url) {
    $ch = $curlHandlerList[$i] = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    ]);

    curl_multi_add_handle($mh, $ch);
}

while (CURLM_OK == curl_multi_exec($mh, $active) && $active) {
    usleep(10**4); // 10**4 microseconds => 0.01 seconds
    curl_multi_select($mh, .01);
}

foreach ($curlHandlerList as $ch) {
    $html = curl_multi_getcontent($ch);

    echo "$html
";
}

是否可以立即访问已经完成的curl处理程序的输出?

1 个答案:

答案 0 :(得分:1)

回答我自己的问题,因为我在任何地方都找不到答案。我没有创建一个多cURL处理程序,而是为每个需要请求的地址创建了一个。参见下面的代码:

$urlList = [                                                            
    'https://httpstat.us/200?sleep=3000',                               
    'https://httpstat.us/200?sleep=1000'                                
];                                                                      

processList($urlList, function($key, $output) {                         
    echo "Output from {$key}\n{$output}\n\n";                             
});                                                                     

function processList($urlList, $closure) {                              

    $curlHandlerList = $multiCurlHandlerList = [];                      

    foreach ($urlList as $i => $url) {                                  
        $ch = $curlHandlerList[$i] = curl_init();                       
        $mh = $multiCurlHandlerList[$i] = curl_multi_init();            

        curl_setopt_array($ch, [                                        
            CURLOPT_URL => $url,                                        
            CURLOPT_HEADER => false,                                    
            CURLOPT_RETURNTRANSFER => true,                             
            CURLOPT_SSL_VERIFYHOST => false,                            
            CURLOPT_SSL_VERIFYPEER => false,                            
        ]);                                                             

        curl_multi_add_handle($mh, $ch);                                
    }                                                                   

    while (count($multiCurlHandlerList)) {                              
        foreach ($multiCurlHandlerList as $i => $mh) {                  
            if (CURLM_OK != curl_multi_exec($mh, $active) || !$active) {

                $html = curl_multi_getcontent($curlHandlerList[$i]);    

                $closure($i, $html);                                    

                curl_multi_remove_handle($mh, $curlHandlerList[$i]);    
                unset($multiCurlHandlerList[$i], $curlHandlerList[$i]); 
            }                                                           
        }                                                               
        usleep(10**4); // 10**4 microseconds => 0.01 seconds            
    }                                                                   
}