[英]ParallelCurl with CURLOPT_WRITEFUNCTION
I am trying to use ParallelCurl with a callback when cURL receives data from the server it is connected to. 当cURL从它连接到的服务器接收数据时,我试图将ParallelCurl与回调一起使用。 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();
Now, what I expect to happen is for my anonymous function to be called when cURL has data to output. 现在,我希望发生的是在cURL有数据输出时调用我的匿名函数。 Instead, it simply seems to ignore the fact that
CURLOPT_WRITEFUNCTION
is set at all. 相反,它似乎完全忽略了
CURLOPT_WRITEFUNCTION
已设置的事实。
Note that if I am not using ParallelCurl, I can set the very same anonymous function as CURLOPT_WRITEFUNCTION just fine. 请注意,如果我不使用ParallelCurl,则可以将CURLOPT_WRITEFUNCTION设置为与匿名函数完全相同的匿名函数。 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)
. 您可以看到我已注释掉的行
//print_r($pc->options)
。 It outputs my closure object. 它输出我的关闭对象。
Any thoughts on this would be most appreciated. 任何对此的想法将不胜感激。 Thanks.
谢谢。
This turned out to be a bug with either ParallelCurl, or curl_set_opt_array(). 事实证明这是ParallelCurl或curl_set_opt_array()的错误。 Here is the function in ParallelCurl as-is:
以下是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();
}
Now the problem lies in where curl_setopt_array($ch, $this->options)
sits. 现在问题出在
curl_setopt_array($ch, $this->options)
所在的位置。 If I move it below all of the other curl_setopt()
, then it works fine. 如果我将其移动到所有其他
curl_setopt()
,那么它将正常工作。 Funny thing is, is that my User-Agent parameter that I pass in the same array as CURLPOT_WRITEFUNCTION
was working fine. 有趣的是,我在与
CURLPOT_WRITEFUNCTION
相同的数组中传递的User-Agent参数工作正常。 So, it seems that curl_setpot_array()
behaves differently when given objects as values in the array. 因此,当给定对象作为数组中的值时,
curl_setpot_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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.