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);
?>
  • Two cURL session handles $ch1 and $ch2 are initialized and configured normally. Note that more than two can be used in this type of operation; two merely satisfy the purpose of this example.
  • A cURL multi handle $mh is initialized and the two session handles are added to it.
  • A loop is used in conjunction with the flag $running to repeatedly check (i.e. poll) the multi handle for completion of all contained operations.
  • curl_multi_getcontent is used on the two session handles to get the response bodies of each.
  • The session handles are individually removed from the multi handle and closed using curl_multi_ remove_handle and curl_close respectively.
  • The multi handle is closed using curl_multi_close.

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 <<<
Category: Article | Added by: Marsipan (30.08.2014)
Views: 371 | Rating: 0.0/0
Total comments: 0
avatar