PHP Class To Interact With API

19th June 2009 - 4 minutes read time

There are a few URL shortening services about, of which is just one. However, it does provide a nice API for developers to interact with the site. Not only is it possible to shorten URL's, you can also reverse them, find out information about the site and get information about how many people have used to link. The API requires you to create an account to interact with the API. Once you have created an account you can use the API service.

Download the bitly API interaction PHP class.

There are several functions available in this class but the main two are shorten() to turn a URL into its equivalent and expand(), which will do the exact opposite.

To convert a URL into its equivalent you can call the shorten() function like this.

$bitly = new bitly('username', 'apikey');
echo $bitly->shorten('');

This will return

To convert a URL back to the original equivalent you can call the expand() function like this.

$bitly = new bitly('username', 'apikey');
echo $bitly->expand('');

This will return the original URL.

The alternate use of the shorten() function is to return an array containing the URL and a hash value returned by The following code.

$bitly = new bitly('username', 'apikey');
print_r($bitly->shorten('', true));

Will return the following array.

    [shortUrl] =>
    [hash] => 5KYMv

This hash code can be used instead of the URL to find out the original URL. The following code will return the original URL.

echo $bitly->expand('', '5KYMv');

You can also change the format of the returning code from using the setFormat() function. However, this has little or no effect on the function of the class itself. To change the format to XML use the following code.


If you want to have a go for yourself I have created a tool that will allow you to convert a URL into a URL.


Pretty nice post. I just came by your site and wanted to say that I've really enjoyed reading your blog posts. In any case I'll be subscribing to your blog and I hope you post again soon!

April (Tue, 06/23/2009 - 23:25)

Any ideas why the XML returned for the "expand" command returns "not well-formed"? See:… (that's the same as the API Wiki's example on with "&format=xml" added at the it should work fine, you'd think) I'll proceed using your JSON solution instead, but any suggested alternatives are welcome, as setting the format in your bitly.php to "xml" returns errors at the moment (although not your fault!).

BitLYDevGuy (Sun, 06/28/2009 - 00:43)

I had a bit of trouble trying to integrate with XML. I think the problem you mention is caused by using 31IqMl as an element name. You can't have a number starting an XML element, so this throws an error. I also had trouble with the error command with XML, which is why the code contains a workaround to use only JSON. The XML returned is formed in such a way that it is not possible to parse it into an array without having to rewrite the XML2Array() function just for that part. It was easier for the time being to just force JSON, since the chances are it would change in the future. What surprises me is that the API is at version 2, you would have thought that they could have sorted out this stuff before now.
Great THANK YOU ! This class is really perfect. I have used it to of automatically convert urls when posting on Twitter using my ggToTwitt application. Using this application you can send post to twitter directly from Gadu-Gadu communicator (most popular in Poland). you for help, Adam

Adam (Tue, 12/01/2009 - 16:17)

my server does not allow file_get_contents() function.

opencode (Sun, 06/06/2010 - 16:38)

I've heard that some hosts have started to block this function, although I'm not sure why really.
There are a few ways to download a web page, here is a (not comprehensive) list of the available ways to do it:
  • The PHP curl library.
  • Snoopy the PHP web browser. Bascially a wrapper for fsockopen.
  • fsockopen().
  • fopen() with feof().
  • fopen() with stream_get_contents().
  • file() and then implode().
  • file_get_contents() function.

The blog post is here:

What I might do is add some functionality that uses an alternate route if the file_get_contents() function isn't available.

The second shorten example that returns an array I believe should be passed a second parameter of true?

Mal Curtis (Mon, 07/19/2010 - 13:04)

Quite right, I have updated the example to include that. Thanks for taking the time to comment! :)
Works like a charm, integrated into my site within minutes thank for sharing it :)

Davie Carr (Sun, 11/14/2010 - 16:32)

Hey Can You Integrate This Class To Get A Shortened URL the Current Page in Which The User Is Surfing? for example using REQUEST_URI

Anonymous (Thu, 01/20/2011 - 14:35)



is this class still work on v3 API from

Hamdi (Mon, 06/27/2011 - 18:25)


Not that I know of. At least I haven't heard of any problems. You could always give it a try and see if it works :)

Add new comment

The content of this field is kept private and will not be shown publicly.