如何将Google短网址转换回原始网址

时间:2015-02-27 10:56:49

标签: php bit.ly goo.gl

我有一些简短的网址,例如 http://goo.gl/fbsfS 。如何使用Ow.lyBit.lygoo.gl从这样的短网址获取原始网址。如果有人拥有php的脚本,那么请帮助我。感谢。

4 个答案:

答案 0 :(得分:4)

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

您可以使用PHP自己的get_headers()函数来获取相应的标题:

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

答案 1 :(得分: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
?>

答案 2 :(得分:0)

您可以使用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);

答案 3 :(得分:-2)

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