cURL WRITEFUNCTION callback with anonymous function and closures
I am using cURL's option for CURLOPT_WRITEFUNCTION to specify a callback to handle when data comes in from a cURL request.
$serverid=5;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.whatever.com');
curl_setopt(
$ch,
CURLOPT_WRITEFUNCTION,
function ($ch, $string) {
return readCallback($ch, $string, $serverid);
}
);
curl_exec($ch);
function readCallback($ch, $string, $serverid) {
echo "Server #", $serverid, " | ", $string;
return strlen($string);
}
I want to use an anonymous function to call my own function that actuall does work (readCallback()
) so that I can include the server ID associated with the request 开发者_开发百科($serverid
). How can I properly utilize closures so that when cURL hits my callback anonymous function, that the anonymous function knows that it was originally defined with $serverid=5
and can call readCallback()
appropriately?
I will eventually be using this with ParalellCurl and a common callback, which is why it is necessary to have an anonymous function call my callback with the ID.
If you would like to have access to $serverid inside your anonymous function, you have to tell PHP that you want to use that variable. Like this:
/*
* I replaced the simple $serverid var to $serverIdHolder because
* 'use' passes the variable by value, so it won't change inside
* your anonymous function that way. But if you pass a reference
* to an object, then you are able to see always the current needed value.
*/
$serverIdHolder = new stdClass;
$serverIdHolder->id = 5;
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.whatever.com');
curl_setopt(
$ch,
CURLOPT_WRITEFUNCTION,
function ($ch, $string) use ($serverIdHolder) {
return readCallback($ch, $string, $serverIdHolder->id);
}
);
curl_exec($ch);
function readCallback($ch, $string, $serverid) {
echo "Server #", $serverid, " | ", $string;
return strlen($string);
}
精彩评论