繁体   English   中英

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

[英]How to convert Google short URLs back to its original URL?

我有一些简短的网址。 如何从中获取原始URL?

由于URL-shortener-services大多数都是简单的重定向器,因此它们使用位置标头来告诉浏览器去哪里。

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

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

尝试这个

<?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
?>

您可以为此使用curl函数

// 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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM