Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2011, 10:26 AM   PM User | #1
Lamped
Super Moderator


 
Join Date: Feb 2009
Location: England
Posts: 539
Thanks: 8
Thanked 63 Times in 54 Posts
Lamped will become famous soon enough
URL Lengthener

Ever wanted to lengthen URLs from URL shorteners before including them in your site? Worry no more!

urlShortener::toLong attempts to look up shortening services and retrieve the original URL. If there's an error, it just returns the short URL. Many issues can cause the original being returned, from unsupported services to malformed output. If it's successful, you'll receive the longer, original URL back.

If a URL has been passed through multiple supported URL shorteners, this code should go through them all until it gets the original.

If there's a URL shortener service not supported here, and you'd like it to be, reply to the post and I'll see what I can do. I've not implemented any service that uses splash pages or frames, and would rather not due to the added complexity.

PHP Code:
define('LAMPED_URLSHORTENER'1);

class 
EURLSHORTENER extends Exception {}
class 
urlShortener {
    const 
supportedTypes ';1u.ro;1url.com;2tu.us;3.ly;adjix.com;altulr.com;b23.ru;bacn.me;bit.ly;cd4.me;chilp.it;chs.mx;cli.gs;cort.as;doiop.com;fa.by;fb.me;flq.us;fly2.ws;fwd4.me;goo.gl;href.in;hurl.ws;idek.net;is.gd;kl.am;korta.nu;lamped.co.uk;liip.to;liltext.com;linkbee.com;liurl.cn;ln-s.net;ln-s.ru;lru.jp;lt.tl;migre.me;miniurl.com;minu.me;www.minu.me;moourl.com;nn.nf;o-x.fr;omf.gd;ow.ly;pnt.me;rb6.me;redirx.com;retwt.me;ri.ms;rickroll.it;short.ie;shrtl.com;simurl.net;simurl.org;simurl.us;sn.im;snipr.com;snipurl.com;snurl.com;sp2.ro;spedr.com;starturl.com;tighturl.com;tiny.cc;tiny.pl;tinyurl.com;to.ly;togoto.us;tr.im;twurl.nl;u.nu;ur1.ca;url.co.uk;url.ie;urlcut.com;urlkiss.com;vl.am;wa9.la;wapurl.co.uk;wipi.es;wp.me;xeeurl.com;xr.com;xrl.in;xrl.us;xurl.jp;ye-s.com;yep.it;zi.pe;zz.gd;';
    static function 
toLong($url) {
        if (
preg_match('/^http:\/\/([a-z0-9\.\-]+)\/([a-z0-9\/_\.\?\-]+)/i'$url$info) && (strpos(self::supportedTypes';'.$info[1].';') !== False)) {
            if (
$handle fsockopen($info[1], 80)) {
                
fwrite(
                    
$handle,
                    
'GET /'.$info[2].' HTTP/1.1'."\r\n".
                    
'Host: '.$info[1]."\r\n".
                    
'User-Agent: Mozilla/5.0 (compatible; LampedWeb/'.LAMPED_URLSHORTENER.')'."\r\n".
                    
'Accept: */*'."\r\n".
                    
'Connection: close'."\r\n".
                    
"\r\n"
                
);
                while (!
feof($handle))
                    
$data .= fgets($handle512);
                
fclose($handle);
                
$data explode("\r\n"$data);
                foreach (
$data as $line)
                    if (
strtolower(substr($line017)) == 'location: http://') {
                        
$redir trim(substr($line10255));
                        if (
preg_match('/^http:\/\/([a-z0-9\.\-]+)/i'$redir$match) && (strpos(self::supportedTypes';'.$match[1].';') !== False))
                            return 
self::toLong($redir);
                        else
                            return 
$redir;
                    }
            }
        }
        return 
$url;
    }

    static function 
getSupported() {
        
$hosts explode(';'self::supportedTypes);
        
sort($hosts);
        
array_shift($hosts); // Account for 2 blank entries from pre/post-fix ;
        
array_shift($hosts);
        return 
$hosts;
    }

Why are there 2 minu.me entries?

minu.me issues a redirect to www.minu.me where the real Location is given. Having both entries allows the internal recursion to recognise www.minu.me

This code is unlicensed and free to use for any purpose, with or without credit.
__________________
lamped.co.uk :: Design, Development & Hosting
marcgray.co.uk :: Technical blog
Lamped is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:55 PM.


Advertisement
Log in to turn off these ads.