2014-10-05 70 views

回答

0

注:以下解决方案是不理想的流量大的场合。

$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”匹配并返回它。

0

使用本

<?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; 
?> 

enter link description here