PHP Class To Interact With bit.ly API

19th June 2009 - 3 minutes read time

There are a few URL shortening services about, of which bit.ly 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 bit.ly equivalent and expand(), which will do the exact opposite.

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

  1. include('bitly.php');
  2. $bitly = new bitly('username', 'apikey');
  3. echo $bitly->shorten('http://www.hashbangcode.com/');

This will return http://bit.ly/Xgb15.

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

  1. include('bitly.php');
  2. $bitly = new bitly('username', 'apikey');
  3. echo $bitly->expand('http://bit.ly/Xgb15');

This will return the original URL.

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

  1. include('bitly.php');
  2. $bitly = new bitly('username', 'apikey');
  3. print_r($bitly->shorten('http://www.hashbangcode.com/', true));

Will return the following array.

  1. Array
  2. (
  3. [shortUrl] => http://bit.ly/Xgb15
  4. [hash] => 5KYMv
  5. )

This hash code can be used instead of the bit.ly 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 bit.ly 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.

$bitly->setFormat('xml');

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

Comments

Permalink
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)

Permalink
Any ideas why the XML returned for the "expand" bit.ly command returns "not well-formed"? See:http://api.bit.ly/expand?version=2.0.1&shortUrl=http://bit.ly/31IqMl&lo… (that's the same as the API Wiki's example on http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/expand with "&format=xml" added at the end...so 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)

Permalink
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.
Permalink
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). http://adzak.com.pl/twitter/Thank you for help, Adam

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

Permalink
my server does not allow file_get_contents() function.

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

Permalink
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:

http://www.hashbangcode.com/blog/quickest-way-download-web-page-php-197.html

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

Permalink
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)

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

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

Permalink
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)

Permalink

Hi,

is this class still work on v3 API from bit.ly?

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

Permalink

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.