3

Create Short URLs Using PHP Functions

Monie on May 12th, 2010 in Tutorials
bitly

Learn how to turn ridicules long links into super short links with Bit.ly with the help of PHP function.

You might have notice that whenever you retweet some post to your twitter account, or whenever you share some link to your twitter account, you will see that your link become something that you don’t quite understand, weird and definitely a lot shorter than it used to be. So, what is that all about?

<a href="http://bit.ly/asSkuN">MonieStudios.com</a>

Introduction To URL Shortening Services

One of the most popular URL shortening services is Bit.ly, which you see a lot in your twitter post link sharing. Bit.ly requires you to sign up for an account and once you have an account, you may attain your login detail, your appkey (you can find your appkey by clicking your username link) and your URL information. That is where you will be doing your URL shortening activity manually.

Most CMS like WordPress have a plugins of built in support for this service where you don’t have to be logged in to Bit.ly in order for you to shorten your link. It will be done right at your page just before it passed on to your twitter account.

What if you want to do the same thing with your custom made website. After all, custom made website doesn’t have shortening URL plugins like WordPress do!

I’ll show you how to create Bit.ly URLs remotely with PHP function.

The Functions

/* make a URL small with bit.ly */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1') {

    //create the URL
    $bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;

    //get the url, could also use cURL here
    $response = file_get_contents($bitly);

    //parse depending on desired format
    if(strtolower($format) == 'json') {
        $json = @json_decode($response,true);
        return $json['results'][$url]['shortUrl'];
    }
     //xml
    else {
        $xml = simplexml_load_string($response);
        return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
    }
}

The Usage

Just put this code where you want to share the page to your social boorkmark (facebook, twitter, etc..). Your new URL that has been shorten can be access with the variable: $short

Example of usage would be something like this:

    // Get the full address of your website page
    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    // Authentication details
    $login = "your.email.id@gmail.com";
    $appkey = "R_22c9bc44570b459ab9d095d7ef5e78fe";

    /* Calling up the function */
    $short = make_bitly_url($url,$login,$appkey,'json');

    /* Share on Twitter link */
    <a class="twitter" href="http://twitter.com/home?status=Your Website Title - <?php echo $short; ?>">Twitter</a>

Happy shortening!

3 Responses so far.

  1. payday loans says:

    This is a superb post Create Short URLs Using PHP Functions.
    But I was wondering how do I suscribe to the RSS feed?

  2. vladimiro13 says:

    it was very interesting to read.
    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

Leave a Reply