如何在重定向URL後獲取鏈接地址?PHP:從重定向的URL獲取鏈接地址
就拿這個網址:http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69
我怎樣才能讓一個PHP腳本回聲最終網址是什麼? (在這種情況下http://www.eltoftnielsen.dk/default.aspx?side=sagsvisning&AutoID=125125&DID=140)
如何在重定向URL後獲取鏈接地址?PHP:從重定向的URL獲取鏈接地址
就拿這個網址:http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69
我怎樣才能讓一個PHP腳本回聲最終網址是什麼? (在這種情況下http://www.eltoftnielsen.dk/default.aspx?side=sagsvisning&AutoID=125125&DID=140)
注:以下解決方案是不理想的流量大的場合。
$url = 'http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69';
file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)\n/', implode("\n", $http_response_header), $matches);
if (isset($matches[0]))
{
echo $matches[0];
}
這裏發生了什麼:file_get_contents()函數重定向和下載目標網站,但原來的響應頭寫入$ http_response_header。
preg_match試圖找到第一個「Location:x」匹配並返回它。
使用本
<?php
$name="19875379";
$url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$header = curl_exec($ch);
$redir = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
//print_r($header);
$x = preg_match("/<script>location.href=(.|\n)*?<\/script>/", $header, $matches);
$script = $matches[0];
$redirect = str_replace("<script>location.href='", "", $script);
$redirect = "http://www.ikea.co.il" . str_replace("';</script>", "", $redirect);
echo $redirect;
?>
使用捲曲。精確的nofollow選項。該網址將位於服務器響應標題中 – 2014-10-05 17:41:25