2

是否有任何类型的 PHP 库可以在不发出 CURL 请求的情况下解码 tinyurl、Goo.gl、bit.ly 和其他 url 缩短的 url?

4

4 回答 4

4

不发出 CURL 请求

通过 URL 缩短,创建的哈希与被缩短的 URL 无关,而是一个简单优雅的内部数据库标识符。

检索 URL 位置的唯一方法是要求链接缩短站点处理请求,然后捕获响应数据,唯一的方法是通过网络连接到该站点。

除非该位置的哈希是长 URL 的可逆哈希,否则没有其他方法可以做到这一点。

于 2011-08-02T00:45:40.240 回答
2

不,这是不可能的。我创建了适用于大多数主机的一小部分功能。

<?php
class url{
        function get_location_header($url){
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
                curl_setopt($ch, CURLOPT_NOBODY, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
                $response = curl_exec ($ch);
                curl_close ($ch);
                preg_match("~(http://.*)~", $response, $match);
                return $match[0];
        }

        function cache_save($file, $content){
                $f = fopen('cache/'.$file, 'w+');
                fwrite($f, $content);
                fclose($f);
        }

        function long_url($short_url){
                $patterns = array('goo\.gl', 'tinyurl\.com', 'bit\.ly');
                $header = $this->get_location_header($short_url);
                if ($header){
                        $long_url = $header;
                        return $long_url;
                }
        }
}

$url = new url();
echo $url->long_url('http://goo.gl/0A3kH').'<br />';
echo $url->long_url('http://tinyurl.com/5b2su2').'<br />';
echo $url->long_url('http://bit.ly/4Agih5');

它是卷曲,但没有卷曲/网络就不可能做到这一点

于 2011-08-02T00:45:28.163 回答
2

至少 bit.ly 和 TinyURL 只使用 HTTP 重定向。

因此,您可以获取 Location 响应标头。例如,您可以使用 PEAR HTTP_Request2。我不知道是否所有服务都使用这种方法,但它是最明显的一种使用......

我不确定你为什么不想使用 curl。您是在谈论特定库或网络库中的 curl 吗?从网页获取信息而不连接到它对我来说似乎相当荒谬:-)

这是不使用库的“最小努力快速'n脏”方法...(您可以grep使用Location标题)...您可以使用套接字完成相同的操作...

[~]% telnet tinyurl.com 80
Trying 64.62.243.89...
Connected to tinyurl.com.
Escape character is '^]'.
GET /69gb3gl HTTP/1.1             
Host: tinyurl.com

HTTP/1.1 301 Moved Permanently
X-Powered-By: PHP/5.3.6
Location: http://www.thinkwithportals.com/media_17.php
X-tiny: db 0.1608510017395
Content-type: text/html
Content-Length: 0
Connection: close
Date: Tue, 02 Aug 2011 00:45:59 GMT
Server: TinyURL/1.6

Connection closed by foreign host.


[~]% telnet bit.ly 80 
Trying 168.143.172.53...
Connected to bit.ly.
Escape character is '^]'.
GET /nm0ZIh HTTP/1.1
Host: bit.ly

HTTP/1.1 301 Moved
Server: nginx
Date: Tue, 02 Aug 2011 00:47:12 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Set-Cookie: _bit=4e374910-002c2-05b82-d5ac8fa8;domain=.bit.ly;expires=Sat Jan 28 19:47:12 2012;path=/; HttpOnly
Cache-control: private; max-age=90
Location: http://richarddawkins.net/videos/642324-iq2-shorts-stephen-fry-vs-ann-widdecombe-catholic-church-debate
MIME-Version: 1.0
Content-Length: 195
于 2011-08-02T00:48:59.423 回答
1

除非您拥有自己的短服务数据库副本,否则您必须提出某种请求。如果不是缩短的 url 本身,那么就是服务的某些 API。好吧,实际上,我想,请求短 url 并获取重定向标头技术上是一个 API。

所以我会说 cURL 它。不要遵循重定向,并确保将 cURL 设置为返回标头(因为没有其他内容)。您正在寻找Location: ....标题

于 2011-08-02T00:46:15.257 回答