Request Pooling
Because it is written C, the cURL extension has one feature that cannot be replicated exactly in libraries written in PHP: the ability to run multiple requests in parallel. What this means is that multiple requests can be provided to cURL all at once and, rather than waiting for a response to be received for the first request before moving on to sending the second, all requests will be sent and processed as responses are returned. This can significantly shorten the time required to collectively complete all the requests. However, care should be taken not to overload a single host with requests when using this feature. <?php $ch1 = curl_init('http://localhost.example/resource1'); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true); /* other curl_setopt calls */ $ch2 = curl_init('http://localhost.example/resource2'); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true); /* other curl_setopt calls */ $mh = curl_multi_init (); curl_multi_add_handle($mh, $ch1); curl_multi_add_handle($mh, $ch2); $running = null; do { curl_multi_exec($mh, $running); } while ($running > 0); $ch1_response = curl_multi_getcontent($ch1); $ch2_response = curl_multi_getcontent($ch2); curl_multi_remove_handle($mh, $ch1); curl_close($ch1); curl_multi_remove_handle($mh, $ch2); curl_close($ch2); curl_multi_close($mh); ?>
Note that this is a fairly simple example that waits until all requests have completed before attempting to process them. If you want to process requests as soon as they return, see the source code at http://code.google.com/p/rolling-curl/source/browse/#svn/trunk for an example of how to do this. © cURL Extension — Web Scraping >>> Back to TABLE OF CONTENTS <<< | |
Views: 419 | |
Total comments: 0 | |