PHP 通用 url 缩短解码器库

发布于 2024-11-27 10:05:40 字数 79 浏览 1 评论 0原文

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

Is there any kind of library for PHP which can decode tinyurl, Goo.gl, bit.ly and other url shortened urls without making CURL requests?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

じее 2024-12-04 10:05:40

不发出 CURL 请求

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

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

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

without making CURL requests

  • No

With URL shortening the hashes created have no relevancy to the URL being shortened but mealy a simple elegant internal database identifier.

The only way to retrieve the URL location is to ask the link shortening site to process the request and then catch the response data, the only way to do that is with networking to that site.

Unless the hash for the location is a reversible hash of long URL, there is no other way to do it.

要走干脆点 2024-12-04 10:05:40

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

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

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

这是不使用库的“最少的努力快速'n dirty”方法......(你可以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

At least bit.ly and TinyURL just uses HTTP redirects.

So you can just get the Location response header. You can use PEAR HTTP_Request2 for example. I don't know if all services use this method, but it is the most obvious one to use...

I'm not sure why you don't want to use curl. Are you talking about curl in specific or networking libraries in general? Getting information from a webpage without connecting to it seems rather non-sensical to me :-)

This is the "minimal effort quick 'n dirty" method that doesn't use a library... (You can grep for the Location header) ... You can accomplish the same using sockets...

[~]% 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
GRAY°灰色天空 2024-12-04 10:05:40

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

<?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则无法做到这一点

No. It isn't possible. I created small pack of function which works on most hosts.

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

it is with curl but it isn't possible to do it without CURL/network

你穿错了嫁妆 2024-12-04 10:05:40

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

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

Unless you have your own copy of the shortner service's database, you have to make some kind of request. If not to the shortened url itself, then to some API of the service. Well, actually, requesting the short url and getting the redirection header is technically an API, I suppose.

So I'd say cURL it. Don't follow redirects, and make sure you set cURL to return the headers (because there's nothing else). You're looking for the Location: .... header

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文