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