ParallelCurl 与 CURLOPT_WRITEFUNCTION [英] ParallelCurl with CURLOPT_WRITEFUNCTION

查看:42
本文介绍了ParallelCurl 与 CURLOPT_WRITEFUNCTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ParallelCurlcURL 从它所连接的服务器接收数据时的回调.这是我目前拥有的代码:

I am trying to use ParallelCurl with a callback when cURL receives data from the server it is connected to. Here is the code I currently have:

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 已设置的事实.

Now, what I expect to happen is for my anonymous function to be called when cURL has data to output. Instead, it simply seems to ignore the fact that CURLOPT_WRITEFUNCTION is set at all.

请注意,如果我不使用 ParallelCurl,我可以设置与 CURLOPT_WRITEFUNCTION 完全相同的匿名函数就好了.就好像我的函数稍后在某个地方被覆盖一样.我还验证了它实际上正在设置.你可以看到我注释掉的那行,//print_r($pc->options).它输出我的闭包对象.

Note that if I am not using ParallelCurl, I can set the very same anonymous function as CURLOPT_WRITEFUNCTION just fine. It as if my function is being overridden somewhere later. I have also verified that it is in fact being set. You can see the line that I have commented out, //print_r($pc->options). It outputs my closure object.

对此的任何想法将不胜感激.谢谢.

Any thoughts on this would be most appreciated. Thanks.

推荐答案

结果证明这是 ParallelCurl 或 curl_set_opt_array() 的错误.这是 ParallelCurl 中的函数原样:

This turned out to be a bug with either ParallelCurl, or curl_set_opt_array(). Here is the function in ParallelCurl as-is:

// 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() 下方,则它可以正常工作.有趣的是,我在 相同数组 中作为 CURLPOT_WRITEFUNCTION 传递的用户代理参数工作正常.因此,当给定对象作为数组中的值时,curl_setpot_array() 的行为似乎有所不同.无论如何,只需移动电话就可以正常工作.我修改的函数:

Now the problem lies in where curl_setopt_array($ch, $this->options) sits. If I move it below all of the other curl_setopt(), then it works fine. Funny thing is, is that my User-Agent parameter that I pass in the same array as CURLPOT_WRITEFUNCTION was working fine. So, it seems that curl_setpot_array() behaves differently when given objects as values in the array. Anyway, simply moving the call worked fine. My modified function:

// 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();
}

这篇关于ParallelCurl 与 CURLOPT_WRITEFUNCTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆