我有一些短网址。如何从他们那里获取原始 URL?
user4613304
问问题
4866 次
4 回答
4
由于 URL-shortener-services 主要是简单的重定向器,它们使用位置标头告诉浏览器去哪里。
您可以使用 PHP 自己的get_headers()
函数来获取相应的标头:
$headers = get_headers('http://shorten.ed/fbsfS' , true);
echo $headers['Location'];
于 2015-02-27T11:23:20.417 回答
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
?>
于 2015-02-27T11:08:49.367 回答
0
您可以使用以下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);
于 2015-02-27T11:17:10.760 回答
-2
对于所有服务,都有一个 API,您可以使用它。
于 2015-02-27T11:01:10.120 回答