首页 > 解决方案 > 是否可以在我的 php 代码中使用多卷曲?

问题描述

我不确定在以下情况下如何或是否可以使用 php multi curl 功能:

我需要卷曲 3 个不同的链接,而它们都相互依赖。使用从第一个链接获得的唯一 ID,我将访问第二个链接以检索另一个唯一 ID,该 ID 将在第三个链接中用于最终结果。请参阅下面的示例以更好地理解:

$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);

我在网上做了很多研究,但找不到合适的东西,我无法理解如何在我的情况下使用 multi curl 或者是否有可能。

希望我对自己要寻找的东西足够清楚。

提前感谢任何能够帮助我的人。

标签: phpurlcurlcookiesexecute

解决方案


简短的回答 - 不。

长答案 - cURL 多句柄用于asynchronous请求,即不相互阻塞的请求。在您的情况下,每个请求都会预先阻止一个请求,因为它取决于其响应,因此它们只能被运行synchronously


推荐阅读