PHP的通用url缩短解码器库


General url shortener decoder library for PHP

是否有任何类型的PHP库可以解码tinyurl, Goo。gl,。ly和其他url缩短url不使CURL请求?

不发出CURL请求

    没有

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

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

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

至少一点。ly和TinyURL只是使用HTTP重定向。

所以你能得到Location响应头。例如,可以使用PEAR HTTP_Request2。我不知道是否所有的服务都使用这种方法,但它是最明显的方法…

我不知道你为什么不想使用curl。您谈论的是特定库中的curl还是一般的网络库中的curl ?从网页获取信息而不连接到它似乎相当荒谬的我:-)

这是不使用库的"最小努力快速肮脏"的方法…(你可以grepLocation头)…您可以使用套接字完成相同的任务…

[~]% 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

No。这是不可能的。我创建了一个小的功能包,可以在大多数主机上运行。

<?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');

有curl,但是没有curl/network是不可能的

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

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