代码之家  ›  专栏  ›  技术社区  ›  Zack Burt

在PHP中确定缩短的URL的最终目的地?

  •  4
  • Zack Burt  · 技术社区  · 16 年前

    如何在PHP中执行此操作?例如

    bit.ly/f00b4r==> http://www.google.com/search?q=cute+kittens

    在Java中,解决方案是:

    你应该向 使用httpwebrequest的URL 实例。在归还中 httpwebresponse,检查 应答器。

    只需确保允许自动重定向 在httpwebrequest上设置为true 实例(默认情况下为真)。 (THX,半胱氨酸天冬氨酸)

    代码是

    private static string GetRealUrl(string url)
    {
        WebRequest request = WebRequest.Create(url);
        request.Method = WebRequestMethods.Http.Head;
        WebResponse response = request.GetResponse();
        return response.ResponseUri.ToString();
    }
    

    (THX,弗雷德里克·莫克)

    但我想用PHP来实现。怎么办?:)

    5 回复  |  直到 9 年前
        1
  •  5
  •   Pascal MARTIN    16 年前

    尝试的时候,你已经找到了答案。

    不过,我还是会这样做的:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://bit.ly/tqdUj");
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    
    $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    
    curl_close($ch);
    
    var_dump($url);
    

    一些解释:

    • 请求的URL是短的
    • 你不想要邮件头
    • 你要确保尸体没有被展示——可能没用
    • 你不想要尸体
    • 当然,您希望跟踪位置
    • 一旦执行了请求,您就需要获取已获取的“真实”URL。

    在这里,你可以得到:

    string 'http://wordpress.org/extend/plugins/wp-pubsubhubbub/' (length=52)
    

    (来自我看到的最后一条包含短网址的推文)


    这应该适用于任何缩短的URL服务,独立于它们的特定API。

    您可能还需要调整一些其他选项,如超时;请参见 curl_setopt 更多信息。

        2
  •  1
  •   Robert French    16 年前
    <?php
    $url = 'http://www.example.com';
    
    print_r(get_headers($url));
    
    print_r(get_headers($url, 1));
    ?>
    
        3
  •  1
  •   Gabriel Sosa    16 年前

    你读过bit.ly API吗?明确地 here ?

    我看不出这个问题。你在说可能的重定向吗?

        4
  •  0
  •   Zack Burt    16 年前

    信贷流向 http://forums.devshed.com/php-development-5/curl-get-final-url-after-inital-url-redirects-544144.html

    function get_web_page( $url ) 
    { 
        $options = array( 
            CURLOPT_RETURNTRANSFER => true,     // return web page 
            CURLOPT_HEADER         => true,    // return headers 
            CURLOPT_FOLLOWLOCATION => true,     // follow redirects 
            CURLOPT_ENCODING       => "",       // handle all encodings 
            CURLOPT_USERAGENT      => "spider", // who am i 
            CURLOPT_AUTOREFERER    => true,     // set referer on redirect 
            CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect 
            CURLOPT_TIMEOUT        => 120,      // timeout on response 
            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 
        ); 
    
        $ch      = curl_init( $url ); 
        curl_setopt_array( $ch, $options ); 
        $content = curl_exec( $ch ); 
        $err     = curl_errno( $ch ); 
        $errmsg  = curl_error( $ch ); 
        $header  = curl_getinfo( $ch ); 
        curl_close( $ch ); 
    
        //$header['errno']   = $err; 
       // $header['errmsg']  = $errmsg; 
        //$header['content'] = $content; 
        print($header[0]); 
        return $header; 
    }  
    $thisurl = "http://www.example.com/redirectfrom";
    $myUrlInfo = get_web_page( $thisurl ); 
    echo $myUrlInfo["url"];
    
        5
  •  0
  •   Dong3000    9 年前

    这是我的解决方案。我对它进行了编码,因为上面的任何一个都不能正常工作。

    function get_final_location($url, $index=null) {
    
        if (is_array($url)) {
            $headers = $url;
        }
        else {
            $headers = get_headers($url, 1)['Location'];    
            if (count($headers) == 0) {
                return $url;
            }
        }
    
        if (is_null($index)) {
            $to_check   = end($headers);
            $index      = count($headers) - 1;
        }
        else {
            $to_check = $headers[$index];
        }
    
        if (!filter_var($to_check, FILTER_VALIDATE_URL) === false) {
            if (count($headers) - 1 > $index) {
                $lp = parse_url($headers[$index], PHP_URL_SCHEME) . "://" . parse_url($headers[$index], PHP_URL_HOST) . $headers[$index+1];
            }
            else {
                $lp = $to_check;
            }
        }
        else {
            $index--;
            $lp = landingpage($headers, $index);
        }
    
        return $lp;
    
    }
    
    推荐文章