ss_blog_claim=88d0a386a6277415f42c9ee5561ded98

Uncloaking TinyURL

Welcome to another PHP tutorial from ForgedEuphoria. This time around we’ll be looking at how to use PHP to input a TinyURL and display the real page’s URL, thus ‘uncloaking’ the link. Let’s get into it!

Here’s the full code for our function:

<?php
function reverseTinyURL($url){
    $url = explode('.com/', $url);
    $url = 'http://preview.tinyurl.com/'.$url[1];
    $preview = file_get_contents($url);
    preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
    return $matches[1];
}
?>

Download this code: revTinyURL.phps

Let’s break it down..

The first line of code declares that it is a function, ‘reverseTinyURL’.

function reverseTinyURL($url){

Next our function splits the URL, starting with .com. Next it gets the source of TinyURL preview page, which is where we will find our uncloaked URL.


$url = explode('.com/', $url);
$url = 'http://preview.tinyurl.com/'.$url[1];
$preview = file_get_contents($url);

Finally it uses the PHP preg_match function to find the line of HTML where our link is hidden and then returns it as a URL.


preg_match('/redirecturl" href="(.*)">/', $preview, $matches);
return $matches[1];
}

If you want to know how to use the function, see below.

<? reverseTinyURL('http://tinyurl.com/2frj9u'); // returns http://www.forgedeuphoria.com/blog/ ?>

Download this code: revTinyURL-example.phps

Pretty neat, eh?

Questions, comments, something unclear? Leave a comment.

If you liked this post, buy me a coffee!


1 Response to “Uncloaking TinyURL”


  1. 1 Adrian Buerki Dec 13th, 2007 at 7:35 pm

    If you like TinyURL, you might also want to check out http://traceurl.com

    TraceURL shortens any URL plus it tracks traffic to the page through the TraceURL shortened web site address, showing where the traffic originated. Count accesses and have the origin of the visitor displayed on a Google Map.

Leave a Reply




October 2007
M T W T F S S
« Sep   Nov »
1234567
891011121314
15161718192021
22232425262728
293031  

RSS  


Posts (RSS) Posts     Comments (RSS) Comments
 Add to Technorati Favorites


advertisements  






top commentators