【问题标题】:How to convert Google short URLs back to its original URL?如何将 Google 短网址转换回其原始网址?
【发布时间】:2015-04-30 01:36:54
【问题描述】:

我有一些短网址。如何从他们那里获取原始 URL?

【问题讨论】:

    标签: php short-url


    【解决方案1】:

    由于 URL-shortener-services 主要是简单的重定向器,它们使用 location header 告诉浏览器去哪里。

    可以使用PHP自带的get_headers()函数来获取相应的header:

    $headers = get_headers('http://shorten.ed/fbsfS' , true);
    echo $headers['Location'];
    

    【讨论】:

    • 谢谢@patrik-mayer。它适用于所有 Ow.ly 、 Bit.ly 、 goo.gl 、 tinyurl。
    • 不客气。也许您应该检查 HTTP 响应以确保存在 301 位置。通常,您的浏览器会被多次重定向。 en.wikipedia.org/wiki/List_of_HTTP_status_codes
    【解决方案2】:

    试试这个

    <?php 
    
    $url="http://goo.gl/fbsfS";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $a = curl_exec($ch); // $a will contain all headers
    
    $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // This is what you need, it will return you the last effective URL
    
    echo $url; // Redirected url
    ?>
    

    【讨论】:

      【解决方案3】:

      您可以为此使用curl functions

      // The short url to expand
      $url = 'http://goo.gl/fbsfS';
      
      // Prepare a request for the given URL
      $curl = curl_init($url);
      
      // Set the needed options:
      curl_setopt_array($curl, array(
          CURLOPT_NOBODY => TRUE,            // Don't ask for a body, we only need the headers
          CURLOPT_FOLLOWLOCATION => FALSE,   // Don't follow the 'Location:' header, if any
      ));
      
      // Send the request (you should check the returned value for errors)
      curl_exec($curl);
      
      // Get information about the 'Location:' header (if any)
      $location = curl_getinfo($curl, CURLINFO_REDIRECT_URL);
      
      // This should print:
      //    http://translate.google.com.ar/translate?hl=es&sl=en&u=http://goo.gl/lw9sU
      echo($location);
      

      【讨论】:

        【解决方案4】:

        对于所有服务,都有一个可供您使用的 API。

        【讨论】:

        • 你说的和他的要求完全相反
        • @NeilMartin:当然不是。使用此 API,您甚至可以获取原始 URL
        • 那你需要清楚你在说什么。此外,他必须为整个互联网中的 URL 缩短器 API 编写不同的代码。考虑实施。满足他的需要是否可行?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-12
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-29
        相关资源
        最近更新 更多