【问题标题】:How to get final redirected url after all redirection of tracking links?在跟踪链接的所有重定向之后如何获得最终重定向的 url?
【发布时间】:2014-01-29 11:22:26
【问题描述】:

我想在 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 的最终重定向 url。 请帮忙。

【问题讨论】:

    标签: php curl


    【解决方案1】:

    试试这个。

            <?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 未加载!

    【讨论】:

    【解决方案2】:

    你可以试试这个代码this url..

    返回重定向的 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);
    

    【讨论】:

      【解决方案3】:

      Goutte 可以为您做到这一点。 https://github.com/FriendsOfPHP/Goutte

      它将遵循 HTTP 3xx 重定向以及 &lt;meta http-equiv="refresh"&gt; 标签。

      示例代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-08
        • 2014-04-13
        • 2014-12-07
        • 2011-03-05
        • 1970-01-01
        • 2015-06-09
        相关资源
        最近更新 更多