Savan Mas
Savan Mas

Reputation: 98

Run other curl when the other curl call is already in progress in PHP

I have to call external script in which i make a first call with CURL to get data which takes about 2-3 minutes, Now during this time i need to make other external call with CURL to get the progress of the first call. But issue is my next call wait till the reply of first CURL comes. I also checked curl_multi but that is also not helping me as i want to make many calls when the first call is in progress. So anyone can help me to solve it please.

Upvotes: 0

Views: 699

Answers (2)

Dileep Kumar
Dileep Kumar

Reputation: 1107

I suppose that, there is no need to make second call to track the CURL progress. You can achieve the same by using CURL option CURLOPT_PROGRESSFUNCTION with a callback function.

The call back method takes 5 arguments:

  1. cURL resource
  2. Total number of bytes expected to be downloaded
  3. Number of bytes downloaded so far
  4. Total number of bytes expected to be uploaded
  5. Number of bytes uploaded so far

In the callback method you can calculate the percentage downloaded/uploaded. An example is given below:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://stackoverflow.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'progress');
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
$html = curl_exec($ch);
curl_close($ch);

function progress($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
    if($download_size > 0)
        echo $downloaded / $download_size  * 100 . "%\n";
    sleep(1);
}

Upvotes: 1

Marc Steven Plotz
Marc Steven Plotz

Reputation: 543

There is a way to do this - please see the following links, they explain how to do this using curl_multi_init: php.net curl_multi_init and http://arguments.callee.info/2010/02/21/multiple-curl-requests-with-php/

Upvotes: 0

Related Questions