ParallelCurl与CURLOPT_WRITEFUNCTION

时间:2011-03-27 23:41:04

标签: php curl anonymous-function

我正在尝试将ParallelCurl与其连接的callback when cURL receives data from the server一起使用。这是我目前的代码:

function request_finished($content, $url, $ch, $user_data) {
    echo "Request Finished: ", $content, "\n";
}

$pc=new ParallelCurl();
$servers=Server::loadNewAllFromDB();  //Returns an array of 'Server' objects which store connection information

foreach ($servers as $server) {

    $pc->setOptions(
        array(
            CURLOPT_USERAGENT=>'My UserAgent String',
            CURLOPT_WRITEFUNCTION=>
                function ($ch, $string) {
                    echo "WRITEFUNCTION Called!  |  ", $string; 
                    return strlen($string); 
                }
        )
    );
    //print_r($pc->options);
    $pc->startRequest(
        'http://' . $server->address . ':' . $server->portbase . '/someurl'), 
        'request_finished'
    );

}

$pc->finishAllRequests();

现在,我期望发生的是当cURL输出数据时调用我的匿名函数。相反,它似乎忽略了CURLOPT_WRITEFUNCTION设置的事实。

请注意,如果我没有使用ParallelCurl,我可以设置与CURLOPT_WRITEFUNCTION完全相同的匿名函数。就好像我的函数稍后被覆盖了一样。我还证实它实际上已被设定。您可以看到我已注释掉的行//print_r($pc->options)。它输出我的闭包对象。

对此的任何想法都将非常感激。感谢。

1 个答案:

答案 0 :(得分:0)

这是ParallelCurl或curl_set_opt_array()的错误。这是ParallelCurl中的函数:

// Start a fetch from the $url address, calling the $callback function passing the optional
// $user_data value. The callback should accept 3 arguments, the url, curl handle and user
// data, eg on_request_done($url, $ch, $user_data);
public function startRequest($url, $callback, $user_data = array(), $post_fields=null) {

    if( $this->max_requests > 0 )
        $this->waitForOutstandingRequestsToDropBelow($this->max_requests);

    $ch = curl_init();
    curl_setopt_array($ch, $this->options);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if (isset($post_fields)) {
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    }



    curl_multi_add_handle($this->multi_handle, $ch);

    $this->outstanding_requests[$ch] = array(
        'url' => $url,
        'callback' => $callback,
        'user_data' => $user_data,
    );

    $this->checkForCompletedRequests();
}

现在问题出在curl_setopt_array($ch, $this->options)所在的位置。如果我将其移动到所有其他curl_setopt()之下,那么它可以正常工作。有趣的是,我通过相同数组传递给我的User-Agent参数CURLPOT_WRITEFUNCTION工作正常。因此,当给定对象作为数组中的值时,curl_setpot_array()似乎表现不同。无论如何,只是移动电话工作正常。我的修改功能:

// Start a fetch from the $url address, calling the $callback function passing the optional
// $user_data value. The callback should accept 3 arguments, the url, curl handle and user
// data, eg on_request_done($url, $ch, $user_data);
public function startRequest($url, $callback, $user_data = array(), $post_fields=null) {

    if( $this->max_requests > 0 )
        $this->waitForOutstandingRequestsToDropBelow($this->max_requests);

    $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if (isset($post_fields)) {
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    }

     curl_setopt_array($ch, $this->options);

    curl_multi_add_handle($this->multi_handle, $ch);

    $this->outstanding_requests[$ch] = array(
        'url' => $url,
        'callback' => $callback,
        'user_data' => $user_data,
    );

    $this->checkForCompletedRequests();
}