如何使用PHP获取Final Redirected Url?

时间:2016-10-01 13:11:23

标签: php redirect curl

我试图在多次重定向后获取最终的url。但是我可以在重定向后只获得下一个立即URL,而不是最后一个。那我怎么得到最后的网址。这是我的代码..

$url = 'http://withOutRedirectedURL.com/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //set url
curl_setopt($ch, CURLOPT_HEADER, true); //get header
curl_setopt($ch, CURLOPT_NOBODY, true); //do not include response body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //do not show in browser the response
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //follow any redirects
curl_exec($ch);
$new_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //extract the url from the header response
curl_close($ch);

echo $new_url;

这段代码给了我下一个url,即如果我有一个重定向,如 url1 - > url2 - > url3 ,我从上面的代码中得到了url2而不是url3。那么如何获得最终的网址?

0 个答案:

没有答案
相关问题