>  기사  >  백엔드 개발  >  CURL获取重定向URL

CURL获取重定向URL

WBOY
WBOY원래의
2016-06-06 20:08:311967검색

使用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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.