2014-01-29 60 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();