2014-01-29 62 views
1

我想在php中所有重定向后得到最终的重定向url。我使用了很多从网络搜索的功能和代码,但没有人对某些跟踪网址是正确的。如何在所有跟踪链接重定向后获得最终的重定向url?

这里是我的网址:http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274

它终于重定向到:https://www.shopandship.com/my-shopandship/profile.aspx

我怎样才能得到这种类型的网址的最终重定向URL。 请帮忙。

回答

2

试试这个。

 <?php 


    function getLastEffectiveUrl($url) 
    { 
     // initialize cURL 
     $curl = curl_init($url); 
     curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_FOLLOWLOCATION => true, 
     )); 

     // execute the request 
     $result = curl_exec($curl); 

     // extract the target url 
     $redirectUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); 
     curl_close($curl); 

     return $redirectUrl; 
    } 

    $url="http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274"; 

    $lastEffectiveUrl = getLastEffectiveUrl($url); 


    if($lastEffectiveUrl==$url) 
    { 
     $url_data=file_get_contents($url); 

     $domHtmlStr = new DOMDocument(); 
     $domHtmlStr->loadHTML($url_data); 
     $metaTag = $domHtmlStr->getElementsByTagName('meta'); 

     foreach($metaTag as $metaTagVal) 
     { 
      if($metaTagVal->getAttribute("http-equiv")=="refresh") 
      { 
      $metaContent = $metaTagVal->getAttribute("content"); 
      break; 
      } 
     } 

     if($metaContent!="") 
     { 
     $metaContentSplit=explode("URL=",$metaContent); 
     $lastEffectiveUrl1 = getLastEffectiveUrl(trim($metaContentSplit[1])); 
     echo "Final URL : ".$lastEffectiveUrl1; 
     } 

    } 

?> 

来源:http://codeaid.net/php/get-the-last-effective-url-from-a-series-of-redirects-for-the-given-url

注:http://performance.ikoo.com/geo_tracking_redirect.html?a=CD441&program_id=1304274没有加载!

+0

不,我在说明书中提到的网址,被采取的某个时候,但重定向 – user3248500

+0

同样的问题与此网址:http://www.awin1.com/awclick.php?mid = 5477&id = 129196 – user3248500

+0

@ user3248500看到我更新的答案。 –

0

你可以试试这个代码this网址..

返回重定向的URL跟踪URL ...

$url = "http://www.awin1.com/awclick.php?mid=5477&id=129196";  
stream_context_set_default(array('http' => array('method' => 'HEAD'))); 
print_r(get_headers($url, 0)); 
$larr = get_headers($url, 1)["Location"]; 

$loc = is_array($larr) ? array_pop($larr):$larr; 
echo $loc."</pre>"; 
$domain = parse_url($loc, PHP_URL_HOST); 
print_r($domain); 
0

GOUTTE能为你做到这一点。 https://github.com/FriendsOfPHP/Goutte

它将遵循HTTP 3xx重定向和<meta http-equiv="refresh">标记。

示例代码:

$client= new \Goutte\Client(); 
$url="http://www.awin1.com/awclick.php?mid=5477&id=129196"; 
echo $client->request("GET",$url)->getUri();