Google URL Shortening Service Class In PHP

Wednesday, January 12, 2011 - 18:48

Google has very recently released access to the API for their URL shortening service (ie, just a couple of days ago) so I have put together a class that allows access to that API. The official site is goo.gl; where you can manually go and enter your own URL's.

Download the goo.gl API interaction PHP class.

The class (and indeed the service) has three main methods, these are shorten(), expand() and analytics(). To get the class up and running you'll need to install CURL and make sure that is can use SSL.

Before you start using the class you might need to go and grab an API key from the Google API Console. I say might as the service will work quite nicely without an API key, you will just get vastly reduced usage limits when you issue commands without a key. To use your key with the class just create the object and pass the key to the constructor, like this:

1
$objGoogl = new Googl('yourgooglapikeyhere');

To shorten a URL use the shorten() method, passing the URL you want to shorten as the single parameter. This method will return the shortened URL (or false if there was an error).

1
2
$shortUrl = $objGoogl->shorten('http://www.hashbangcode.com/');
print $shortUrl; // Prints out 'http://goo.gl/Y5Z0R'

To expand a goo.gl URL to the original URL use the expand() method, with the shortened URL as the single parameter. This will return the original URL (or false if there was an error).

1
2
$url = $objGoogl->expand('http://goo.gl/fbsS');
print $url; // Prints out 'http://www.google.com/'

The service also provides some analytics tracking that can be downloaded using the analytics() method. This method takes a single parameter of a shortened URL and will return the data from the service. This is in the form of a PHP traversable object and there is quite a bit of data there so it is probably best that you download it and take a look at what sort of data you can extract.

1
$analytics = $objGoogl->analytics('http://goo.gl/fbsS');

The class will throw exceptions for anything out of the ordinary, but if the goo.gl site returns an error then all of the above functions will return false. To see what errors came back you can use the getErrors() method, which will return the errors returned so you can see what happened and act accordingly.

Finally, whilst writing this class I wrote some unit tests, feel free to download them from the link below and test the code for yourself. Note that you will need to include your API key at the top of this file to test things properly.

Download the googl API interaction PHP class unit tests.

The Google URL shortener service can also use an auth token to make the shortened URL's you create unique. It will also cause the URL to show up in your dashboard at goo.gl, giving you lots more information about the URL. I have left that out of this class for now, but will probably look at integrating it in the future (once I have figured out how it works).

This class is currently hard coded to use version 1 of their API, but as there isn't a version 2 I won't worry about this for now, especially as I can't guess what it would do differently. If you use this class in your code then please drop by and leave a comment about how useful it was to you.

For more information on the Google URL shortening service take a look at goo.gl and the API documentation

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

Thank you so much for your example. I spent two days battling to get my code to work on a testing server, and none of the examples / tutorials I could find solved my problem.

Add new comment