php - 是否可以在我的 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 或者是否有可能。
希望我对自己要寻找的东西足够清楚。
提前感谢任何能够帮助我的人。
解决方案
简短的回答 - 不。
长答案 - cURL 多句柄用于asynchronous
请求,即不相互阻塞的请求。在您的情况下,每个请求都会预先阻止一个请求,因为它取决于其响应,因此它们只能被运行synchronously
。
推荐阅读
- java - JavaFX如何制作动态相机,如何限制视口可见性
- c++ - 在非实例化模板中使用 static_assert 的正确方法是什么?
- json - 在 play scala 的单元测试中模拟 WsClient
- python - 我无法删除产品
- ios - iOS 订阅 latest_receipt_info
- visual-studio-code - VS Code 无法启动 Bash
- botframework - 创建访问器后无法写入机器人状态属性
- python - ValueError:不支持 ssd_inception_v3。有关与不同版本 TensorFlow 兼容的特征提取器,请参阅 `model_builder.py`
- javascript - 如何使用 v-for vuejs 指令获取元素 ID?
- jmeter - 根据请求为变量赋值