发送多个cURL并强制代码不等待响应

时间:2017-01-12 14:25:28

标签: php api curl

当我发送cURL请求时,如何阻止我的功能回复响应?我基于http://php.net/manual/en/function.curl-multi-exec.php文档开发了这个函数。然而,当我尝试发送超过50条消息时,我正在开发的应用程序冻结在发送按钮超过20秒。有没有办法发送多个cURL并强制代码不等待响应?

function send_multi_data($url,$data){
//create the multiple cURL handle
$mh = curl_multi_init();
$active = null;
$count=0;

// set URL and other appropriate options
foreach($data as $message){
    $count++;
    $ch[$count] = curl_init();
    curl_setopt($ch[$count], CURLOPT_URL, $url);
    curl_setopt($ch[$count], CURLOPT_HEADER, 0);
    curl_setopt ($ch[$count], CURLOPT_POSTFIELDS, $message); 
    curl_multi_add_handle($mh,$ch[$count]);
}
//execute the handles
    do {
        $mrc = curl_multi_exec($mh, $active);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) {
        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

// Close the handle
$count1=0;
foreach($data as $message){
    $count1++;
    curl_multi_remove_handle($mh, $ch[$count1]);
}
curl_multi_close($mh);

}

0 个答案:

没有答案