PHP curl_multi_exec运行一次

时间:2011-02-22 22:25:34

标签: php curl curl-multi

我在使用php的curl_multi_exec创建多个xml请求时遇到问题。

问题是包含curl_multi_exec命令的do ... while循环只运行一次然后退出。

使用的资源:

http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/ http://php.net/manual/en/function.curl-multi-exec.php/ http://www.rustyrazorblade.com/2008/02/curl_multi_exec/

看看我的代码:

    //Multi handle curl initialization
    $mh = curl_multi_init();

    //set url
    $url = 'my_url';

    foreach($latLng as $id => $l) {
        $ch[$id] = curl_init();

        //$request previously set                

        //Initialize and set options 
        curl_setopt($ch, CURLOPT_URL,                $url);
        curl_setopt($ch, CURLOPT_HEADER,             0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,     1);
        curl_setopt($ch, CURLOPT_POST,               1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,        $request); 

        //add to multi_handle
        curl_multi_add_handle($mh, $ch[$id]);
    }

    //Execute the handles
    $running = null;

    do {
        $mrc = curl_multi_exec($mh, $running);   
        $ready=curl_multi_select($mh);
        echo "Ran once\n";
    } while ($mrc == CURLM_CALL_MULTI_PERFORM && $ready > 0);

    while ($active && $mrc == CURLM_OK) {
        if ($curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $running);
                echo "Ran again\n";
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    foreach ($mh as $c) {
        // HTTP response code
        $code = curl_getinfo($c,  CURLINFO_HTTP_CODE);

        // cURL error number
        $curl_errno = curl_errno($c);

        // cURL error message   
        $curl_error = curl_error($c);

        // output if there was an error                                                            
        if ($curl_error) {
            echo("*** cURL error: ($curl_errno) $curl_error\n");
        }
    }

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

    print_r($result);

    //Close curl
    curl_multi_close($mh);
}

我知道请求是有效的,因为当我执行单个curl执行时,我收到了正确的返回数据。问题在于curl_multi_exec()。

我收到的输出是“Ran once”,然后是curl_multi_getcontent()调用的空数组。见下文:

跑了一次 排列 (     [0] =>     [1] =>     [2] =>     [3] =>     [4] =>     [5] =>     [6] =>     [7] =>     [8] => )

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

您没有正确设置curl选项:

目前,您正在设置$ch这个数组的选项,您需要在当前的curl处理程序上专门设置选项,这在您的循环中为$ch[$id]

   //Initialize and set options 
   curl_setopt($ch[$id], CURLOPT_URL,                $url);
   curl_setopt($ch[$id], CURLOPT_HEADER,             0);
   curl_setopt($ch[$id], CURLOPT_RETURNTRANSFER,     1);
   curl_setopt($ch[$id], CURLOPT_POST,               1);
   curl_setopt($ch[$id], CURLOPT_POSTFIELDS,        $request); 

答案 1 :(得分:0)

改变这个:

foreach ($mh as $c) {
    $code = curl_getinfo($c,  CURLINFO_HTTP_CODE);

为:

for($i=1;$i<=count($array);$i++){
    $code = curl_multi_getcontent($ch[$i]); 

假设$ array是多个$ url的数组。