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.