It can be an anonymous function:
$rc = new RollingCurl();
$rc->setCallback(function($request, $rolling_curl) {
process
});
Or an existing function:
class MyClass {
function doCurl() {
$rc = new RollingCurl();
$rc->setCallback(array($this, 'callback'));
}
Cannot be private or protected
public function callback($request, $rolling_curl) {
process
}
}
The called code should expect two parameters: \RollingCurl\Request $request, \RollingCurl\RollingCurl $rollingCurl
$request is original request object, but now with body, headers, response code, etc
$rollingCurl is the rolling curl object itself (useful if you want to re/queue a URL)
public setCallback ( callable $callback ) : |
||
$callback | callable | |
return |
public function performAction() { $rollingCurl = new RollingCurl(); foreach ($this->getPerlTablesUrlList() as $url) { $rollingCurl->get($url); } $rollingCurl->setCallback(function (Request $request, RollingCurl $rollingCurl) { $this->response->addContent($request->getUrl()); $content = $request->getResponseText(); $this->parsePerlTable($content); })->execute(); }
/** * Sends rolling curl multirequest. * @param array $data curl request data as [url, url, ...] or [url=>['post'=>[...], 'files'=>[...]]] * @param \Closure $callback callback at response. */ public function multiRequest($data, $callback) { $rollingCurl = new RollingCurl(); foreach ($data as $url => $options) { if (is_string($options)) { $url = $options; } if ($this->unique && in_array($url, $this->visited) && !@$options['post']) { continue; } else { $this->visited[] = $url; } $method = isset($options['post']) ? 'POST' : 'GET'; $request = new Request($url, $method); $curlOptions = $this->getCurlOptions($url, @$options['post'], @$options['files']); $request->setOptions($curlOptions); $rollingCurl->add($request); } $rollingCurl->setCallback($callback)->execute(); }