Introduction
Why we are discussing this(multiple cURL requests/curl_multi_exec).
Let’s take an example where we need to fetch records from the server based on ids assuming that server will return data for single id in a single request.
Now, if we use cURL then we need to iterate loop for each id and request server therefore if there are 10 ids then on an average response time will be 10X of a single request.
/** * Webkul Software. * * @category Webkul * @author Webkul * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ $result=array(); foreach ($ids as $id) { // URL from which data will be fetched $fetchURL = 'https://webkul.com&customerId='.$id; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $fetchURL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result[] = curl_exec($ch); curl_close($ch); }
Solution
But if use multiple cURL requests then response time reduces significantly.
/** * Webkul Software. * * @category Webkul * @author Webkul * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ // array of curl handles $multiCurl = array(); // data to be returned $result = array(); // multi handle $mh = curl_multi_init(); foreach ($ids as $i => $id) { // URL from which data will be fetched $fetchURL = 'https://webkul.com&customerId='.$id; $multiCurl[$i] = curl_init(); curl_setopt($multiCurl[$i], CURLOPT_URL,$fetchURL); curl_setopt($multiCurl[$i], CURLOPT_HEADER,0); curl_setopt($multiCurl[$i], CURLOPT_RETURNTRANSFER,1); curl_multi_add_handle($mh, $multiCurl[$i]); } $index=null; do { curl_multi_exec($mh,$index); } while($index > 0); // get content and remove handles foreach($multiCurl as $k => $ch) { $result[$k] = curl_multi_getcontent($ch); curl_multi_remove_handle($mh, $ch); } // close curl_multi_close($mh);
Result for each cURL request is in $result array some what like this:
$result[0] => {"id1":{"customerName":"John","customerAddress":... $result[1] => {"id2":{"customerName":"JDoe","customerAddress":... ....
This is my GitHub: https://github.com/ljalvrdz/mex_sat
I would apreciate it I can’t seem to find the answer to make this new multiCurl working, and making one request at the time takes to long when many records to validate.
Thanks!