PHP Class To Interact With bit.ly API

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.

include('bitly.php');
$bitly = new bitly('username', 'apikey');
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.

include('bitly.php');
$bitly = new bitly('username', 'apikey');
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.

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

Will return the following array.

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

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 download the class and give it a go.

Comments

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!
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!).
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.
Name
Philip Norton
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
Permalink
my server does not allow file_get_contents() function.
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.

Name
Philip Norton
Permalink
The second shorten example that returns an array I believe should be passed a second parameter of true?
Permalink
Quite right, I have updated the example to include that. Thanks for taking the time to comment! :)
Name
Philip Norton
Permalink
Works like a charm, integrated into my site within minutes thank for sharing it :)
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
Permalink
You'll need to use the currentUri() function that I wrote about in another post to get the full URI of the page (http://www.hashbangcode.com/blog/getting-current-uri-php-121.html). After this you just need to use it as part of the shorten() method. Like this:
$bitly->shorten(currentUri(), true)
There are probably other functions available, but this will get you what you need :)
Name
Philip Norton
Permalink

Hi,

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

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

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
6 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.