a simple curl wrapper for the multi handler
This a (light) abstract layer around the curl multi for handling asynchronously request., (*1)
The purpose of this library is to make it easier to handle to handle to responses but trying to keep as close to curl as possible. This should keep the api more stable, easier to work with (when familiar with curl) and less code around curl., (*2)
The provided request is therefor not more than an array like you wold register with curl_setopt_array
. So urls, header
etc. should be registered with the curl methods CURLOPT_URL, CURLOPT_WRITEHEADER, CURLOPT_HTTPHEADER and rest could be
found here., (*3)
use PBergman\CurlMulti\MultiHandler; use PBergman\CurlMulti\Request; $null = fopen("/dev/null", "w"); $handler = new MultiHandler( new Request([ CURLOPT_URL => "http://httpstat.us/200?sleep=5000", CURLOPT_FILE => $null, ]), new Request([ CURLOPT_URL => "http://httpstat.us/404", CURLOPT_FILE => $null, ]) ); try { foreach ($handler->getResponse() as $response) { printf( "request '%s' finished with code %d in %0.2fs\n", $response->getInfo(CURLINFO_EFFECTIVE_URL), $response->getInfo(CURLINFO_RESPONSE_CODE), $response->getInfo(CURLINFO_TOTAL_TIME) ); } } finally { // register cleanup $handler->close(); }
should give a output like:, (*4)
request 'http://httpstat.us/404' finished with code 404 in 0.45s request 'http://httpstat.us/200?sleep=5000' finished with code 200 in 5.49s
constructor of MultiHandler accepts request so you could register your request on creating a new instance., (*5)
(new MultiHandler( new Request([ CURLOPT_URL => "http://httpstat.us/200?sleep=5000", CURLOPT_FILE => $null, ]), new Request([ CURLOPT_URL => "http://httpstat.us/404", CURLOPT_FILE => $null, ]) ))->wait();
set options for multi handler, (*6)
set option for multi handler, (*7)
add request to defined instance., (*8)
register the close function for shutdown., (*9)
will setup the curl multi hadle, this will be called when creating a new instance and can be useful when the close method was called., (*10)
returns a finished request and/or block until one is finished., (*11)
wait till all request are finished, (*12)