CURL获取重定向URL
程序员文章站
2023-12-31 17:02:04
...
使用CURL获取下面链接重定向URL:
http://www.amazon.com/Bengoo-Portable-Desktop-Electric-Rechargeable/dp/tech-data/B01F70ZGYW%3FSubscriptionId%3DAKIAJWXT2MCY6ZQDW7VQ%26tag%3DASSOCIATETAG%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB01F70ZGYW
我抓取到重定向后的链接还是这条url。
而实际的URL为:
http://www.amazon.com/dp/B01F70ZGYW?_encoding=UTF8&SubscriptionId=AKIAJWXT2MCY6ZQDW7VQ&showDetailTechData=1&tag=ASSOCIATE#technical-data
http://www.amazon.com/Bengoo-Portable-Desktop-Electric-Rechargeable/dp/tech-data/B01F70ZGYW%3FSubscriptionId%3DAKIAJWXT2MCY6ZQDW7VQ%26tag%3DASSOCIATETAG%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB01F70ZGYW
我抓取到重定向后的链接还是这条url。
而实际的URL为:
http://www.amazon.com/dp/B01F70ZGYW?_encoding=UTF8&SubscriptionId=AKIAJWXT2MCY6ZQDW7VQ&showDetailTechData=1&tag=ASSOCIATE#technical-data
回复内容:
使用CURL获取下面链接重定向URL:
http://www.amazon.com/Bengoo-Portable-Desktop-Electric-Rechargeable/dp/tech-data/B01F70ZGYW%3FSubscriptionId%3DAKIAJWXT2MCY6ZQDW7VQ%26tag%3DASSOCIATETAG%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB01F70ZGYW
我抓取到重定向后的链接还是这条url。
而实际的URL为:
http://www.amazon.com/dp/B01F70ZGYW?_encoding=UTF8&SubscriptionId=AKIAJWXT2MCY6ZQDW7VQ&showDetailTechData=1&tag=ASSOCIATE#technical-data
$url = "http://www.amazon.com/Bengoo-Portable-Desktop-Electric-Rechargeable/dp/tech-data/B01F70ZGYW%3FSubscriptionId%3DAKIAJWXT2MCY6ZQDW7VQ%26tag%3DASSOCIATETAG%26linkCode%3Dsp1%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB01F70ZGYW";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Fiddler");
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
echo ! empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
推荐阅读
-
CURL获取重定向URL
-
javascript parseUrl函数(来自国外的获取网址url参数)_javascript技巧
-
PHP中使用CURL获取页面title例子_php技巧
-
怎么把CURL获取到的图片以二进制的方式post到某地址
-
php获取URL中带#号等特殊符号参数的解决方法
-
怎么使用PHP,获取图片跳转后的URL地址
-
PHP获取现阶段url路径的函数:QUERY_STRING、REQUEST_URI、SCRIPT.
-
PHP中使用CURL模拟登录并获取数据实例_php实例
-
php的curl获取有301/302跳转文件出错问题解决
-
验证码-php中如何用curl模拟http头信息进行模拟登陆并获取信息