Thursday 20 December 2018

PHP CURL parallel && write download content on the fly using curl

Apache parallel command:

apt-get install parallel
#execute 4 parallel requests in total, 2 parallel request to be executed at atime
seq 4 | parallel -n0 -j2 "curl -H 'Content-Type: application/json'  -X GET http://172.16.107.209/~jxiang/response_GET.php"
#execute 4 parallel requests in total, 4 parallel request to be executed at atime
seq 4 | parallel -n0  "curl -H 'Content-Type: application/json'  -X GET http://172.16.107.209/~jxiang/response_GET.php"



PHP parallel curl(curl multi):
/**
* 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);


 // Open file to write donwload content
                                $fp = fopen('fortipoc_export_all.poc', 'w+');
                                curl_setopt($this->_curl, CURLOPT_FILE, $fp);

                                curl_exec($this->_curl);

No comments:

Post a Comment