开发者

General url shortener decoder library for PHP

开发者 https://www.devze.com 2023-03-25 09:37 出处:网络
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?

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?


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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号