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.
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.
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.
- The PHP curl library.
- Snoopy the PHP web browser. Bascially a wrapper for 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.
$bitly->shorten(currentUri(), true)There are probably other functions available, but this will get you what you need :)
is this class still work on v3 API from bit.ly?
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 :)