Request quote

Easily create short URLS with php, curl and or

Posted on: April 21st, 2012 by Raez Mon 7 Comments

The 140 character limit in twitter has made the necessity of URL shortner services. There are many URL shortening services available like,, http://ow.l etc.. You can even have your own short URL service, if you are so much worried about your brand. Deciding which type to use is surely a decision based on your business and revenue stream. If you are getting a majority of your business on Twitter, or if you have an extremely long URL and need to shorten it up for print, social media, television, or other advertising to help make it memorable, then a custom shortened URL is probably the way to go for you. Also, if you want 100 percent control over your content and your identity, then you should also be shopping for your own shortened identity.

For the rest of you, maybe will suffice, or maybe registering hybrid or customized URL will fill the gap. Through this tutorial we are going to help these sort of people who are not so worried about their brands.

How to create short URL with and php

It is easy to create short URL’s with Google’s URL shortening service, Use the following code. You need to have curl enabled in your server. You also need to get an API Key from

 $longUrl, 'key' =>; $GoogleApiKey);
    $jsonData = json_encode($postData);
    $curlObj = curl_init();
    curl_setopt($curlObj, CURLOPT_URL, '');
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    //As the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.
    curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curlObj, CURLOPT_HEADER, 0);
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    curl_setopt($curlObj, CURLOPT_POST, 1);
    curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
    $response = curl_exec($curlObj);
    $json = json_decode($response);
    return $json->id;

Now you can get the short URL for your long URL by simply

How to create short URL with and php

You can also use the service of another popular URL shortening service. The only demerit with them is that, you need to have an account with them. Here is the php code to create short URLs with

To show the short URL, simply

Thats it. If time permits, we will add codes for doing the same with other service providers too. Also the code for the reverse too, that is to get the original URL from a short URL.

We have released a plugin for elgg with the above code, which you can download from the elgg community.

Tags: , , , , , ,

7 Responses

  1. Elenara says:

    the info shared in this blog is very interesting, i hope everyone like it.

  2. Very good this code, but I would like to know how to adapt it to the button-sharing links on Twitter. Can you help me?
    The code that I use is this:

    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);;js.src=”//”;fjs.parentNode.insertBefore(js,fjs);}}(document,”script”,”twitter-wjs”);

  3. Santiago says:

    the bitly example works fine

  4. Ipixler says:

    Working good.
    But i wonder if url shortener can improved SEO for backlink?
    I want to use it for my website Images Search Engine for some internal url but using

  5. Team Webgalli'an says:

    Thanks for the confirmation.

  6. Y.Milenkov says:

    Nice and easy :)

  7. RĂ´mulo Neves says:

    In google case, API Key isn’t necessary!