0

I am not sure how can or if I can use php multi curl function in the following situation:

I need to curl 3 different links while all of them are dependent to each other. With the unique id obtained from the first link I will access the second link to retrieve another unique id which will be used in the 3rd link for the final result. See example below for better understanding:

$agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";

//open the session with the first link
$url1 = "http://first-link-example.com/execute";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results = curl_exec($ch);

//get unique id from the link above
$unique1 = explode("string1", $results);
$unique1 = explode("string2", $unique1[1]);


//new request to access second link while preserving the session
$url2 = "http://second-link-example.com/execute?id=".$unique1[0];

curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results2 = curl_exec($ch);

//get the second unique id from $results2 page
$unique2 = explode("string1", $results2);
$unique2 = explode("string2", $unique2[1]);

//final request to retrieve the desired string while preserving the session
$url3 = "http://third-link-example.com/execute?id=".$unique2[0]."/response";

curl_setopt($ch, CURLOPT_URL, $url3);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
$results3 = curl_exec($ch);

//get the desired string from $results3 page
$success = explode("string1", $results3);
$success = explode("string2", $success[1]);

echo $success[0];

curl_close($ch);

I did a lot of research on the web but couldn't find something appropriate and I wasn't able to understand how can I use multi curl in my situation or if it's possible.

Hopefully I made myself clear enough regarding what I'm looking for.

Thanks in advance to anyone who will be able to help me.

Iain
  • 3
  • 1
  • Does this answer your question? [PHP Multiple Curl Requests](https://stackoverflow.com/questions/3900153/php-multiple-curl-requests) – Labradorcode Nov 14 '19 at 13:17
  • I don't think in your case it would be suitable to use `curl_multi_*` functions if the results from one are required for the input to a subsequent curl call - you want them to run sequentially I'd have thought rather than in parallel – Professor Abronsius Nov 14 '19 at 13:21
  • Use promises or some other async technology for this otherwise you will have race conditions – Kingalione Nov 14 '19 at 13:23

1 Answers1

2

Short answer - NO.

Long answer - cURL multi handles are intended for asynchronous requests, i.e requests that don't block each other. In your case, each request blocks the one beforehand, as it is dependent on its response, so they must only be ran synchronously.

mariobgr
  • 2,143
  • 2
  • 16
  • 31
  • I understand. Many thanks for clarifying this. I've had a similar thought but needed an pro opinion because couldn't find something similar on the web in my researches. P.S. is there any solution I could speed up the script because it's taking quit long to execute? – Iain Nov 14 '19 at 13:26
  • The execution time depends on many factors, but in your case the script itself is so simple, that the main factor would be the time for your server to connect to the remote server and to get the response back. I'd say the best thing to do would be to access the resource over the local network (if possible) or use a server with a better connectivity to the resource. – mariobgr Nov 14 '19 at 13:30