Home  >  Article  >  Backend Development  >  CURL获取重定向URL

CURL获取重定向URL

WBOY
WBOYOriginal
2016-06-06 20:08:311967browse

使用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

回复内容:

使用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

<code>$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';</code>

<code>    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn