【问题标题】:Determine Final Destination of a Shortened URL in PHP?在 PHP 中确定缩短 URL 的最终目的地?
【发布时间】:2009-08-23 21:33:22
【问题描述】:

如何在 PHP 中做到这一点?例如

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

在 Java 中,解决方案是这样的:

您应该发出 HEAD 请求 使用 HttpWebRequest 的 url 实例。在返回的 HttpWebResponse,检查 ResponseUri。

只需确保 AllowAutoRedirect 在 HttpWebRequest 上设置为 true 实例(默认为真)。 (谢谢,casperOne)

代码是

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

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

但我想用 PHP 来做。如何? :)

【问题讨论】:

  • 你需要这个来做特定的起酥油吗?或者你只是在说一般?当通过 API 调用时,许多“好”的缩短器可以为您提供最终目的地
  • 仅供参考:如果您试图让t.co twitter url 重定向,他们会给出 200 响应代码,因此您必须从正文中获取 <noscript><META http-equiv="refresh" content="0;URL=http://tinyurl.com/7ktoegq"></noscript><script>location.replace("http:\/\/tinyurl.com\/7ktoegq")</script>

标签: php url-routing bit.ly


【解决方案1】:

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

不过,我还是会选择这样的:

$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 很短
  • 你不想要标题
  • 您想确保不显示正文——可能没用
  • 你不想要身体;即,你想要一个 HEAD 请求,而不是 GET
  • 您当然希望跟踪位置
  • 一旦请求被执行,您想要获取已获取的“真实”URL

而且,在这里,你得到:

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

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


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

您可能还想调整一些其他选项,例如超时;请参阅curl_setopt 了解更多信息。

【讨论】:

  • 我在 t.co 推特链接上尝试了一些东西,这是迄今为止唯一对我有用的东西。谢谢! +1
【解决方案2】:
<?php
$url = 'http://www.example.com';

print_r(get_headers($url));

print_r(get_headers($url, 1));
?>

【讨论】:

  • 解析 Location 标头可能会起作用;但是如果有两个(或更多)级别的重定向怎么办? (是的,不是您通常看到的,但是如果目标站点在发布新版本站点的那天设置了一些重定向怎么办?)
【解决方案3】:

您是否阅读过 bit.ly API?特别是here

我看不到问题。您是在谈论可能的重定向吗?

【讨论】:

  • 如果使用特定的 API,您必须为每个不同的缩短 URL 服务创建一个新的特定代码;考虑到其中有很多,您将永远不会停止编码和测试......一些适用于任何服务的“通用”解决方案可能会更容易,至少作为一个长期解决方案......
【解决方案4】:

信用转到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】:

    这是我的解决方案。我编码了它,因为以上都不能正常工作。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多