【发布时间】:2020-09-23 11:28:40
【问题描述】:
我想获取 url 的完整重定向路径。
假设 source.com 在多次重定向后重定向到destination.com,如下所示: http://www.source.com/ -> http://www.b.com/ -> http://www.c.com/ -> http://www.destination.com/
如何获取所有重定向的 URL?
使用下面的代码我只得到http://www.destination.com/ 如何检测完整的 url 重定向链?
<?php
$url='windows.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow the redirects
curl_setopt($ch, CURLOPT_HEADER, false); // no needs to pass the headers to the data stream
curl_setopt($ch, CURLOPT_NOBODY, true); // get the resource without a body
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // accept any server certificate
curl_exec($ch);
// get the last used URL
$lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
echo $lastUrl;
?>
此代码还有一个问题,它无法检测 youtube 重定向的重定向 url。
测试网址:https://www.youtube.com/redirect?redir_token=QUFFLUhqbkVxUFZUME9NbWF4RThxdFpGV3pmTTJEdFVWQXxBQ3Jtc0tubGJqU016TzJ6WnlfeUItX0ZmOUItUE1jRlZoZXhxMzNpQllpM0NLSk4ycnBLMGNidTFsX3N6WkU2X3RsUTRZb1lXQVp5SEZjbnU3eDFuZS1VU3dhdzg2QW9ZMTl1azFCZFZHcHRLdFF3dTM1MlRWdw%3D%3D&event=video_description&v=KEa2XWRGf_4&q=https%3A%2F%2Fwww.facebook.com%2Fabhiandniyu
我的问题是如何为所有类型的重定向请求检测完整的 url 重定向链。
【问题讨论】:
-
到目前为止您尝试过什么?你被困在哪里了?编写普通的 cURL 命令会使你的代码膨胀很多,那么为什么不使用像 Guzzle 这样的库呢?