【发布时间】:2015-04-30 01:36:54
【问题描述】:
我有一些短网址。如何从他们那里获取原始 URL?
【问题讨论】:
我有一些短网址。如何从他们那里获取原始 URL?
【问题讨论】:
由于 URL-shortener-services 主要是简单的重定向器,它们使用 location header 告诉浏览器去哪里。
可以使用PHP自带的get_headers()函数来获取相应的header:
$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 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);
【讨论】:
对于所有服务,都有一个可供您使用的 API。
【讨论】: