我必须从此处获取最终的重定向网址:https://web.archive.org/web/20070701005218/http://www.maladnews.com/实际重定向到此:https://web.archive.org/web/20080109064420/http://www.maladnews.com/Site%203/Malad%20City%20&%20Oneida%20County%20News/Malad%20City%20&%20Oneida%20County%20News.html
我尝试了其他stackoverflow答案的答案,这些答案适用于其他网站,但不适用于上述链接。
我试图提取常规位置标题:
if(preg_match('#Location: (.*)#', $html, $m))
$l = trim($m[1]);
并检查了javascript方式:
preg_match("/window\.location\.replace\('(.*?)'\)/", $html, $m) ? $m[1] : null;
请帮忙!
答案 0 :(得分:3)
根据您的使用情况,CURLINFO_REDIRECT_URL
或CURLINFO_EFFECTIVE_URL
使用curl_getinfo()
。
中找到
CURLINFO_REDIRECT_URL
- 禁用CURLOPT_FOLLOWLOCATION
选项:在上次交易中找到重定向网址,接下来应手动申请。启用CURLOPT_FOLLOWLOCATION
选项后:此项为空。在这种情况下,重定向网址可在CURLINFO_EFFECTIVE_URL
- http://php.net/manual/en/function.curl-getinfo.php
<?php
$url = 'https://google.com/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$html = curl_exec($ch);
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
echo "Original URL: " . $url . "\n";
echo "Redirected URL: " . $redirectedUrl . "\n";
当我运行此代码时,输出为:
Original URL: https://google.com/
Redirected URL: https://www.google.com/