Shortening URLs from PHP with Bit.ly
Here’s a simple example, using PHP’s curl extension, of using the bit.ly API to get a short URL, using PHP (you need an API key, but if you’re a registered bit.ly user, you can log in and then find yours at http://bitly.com/a/your_api_key).
$ch = curl_init('http://api.bitly.com/v3/shorten?login=username&apiKey=R_secret&longUrl=http%3A%2F%2Flornajane.net');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r(json_decode($result));
The default format of this API is JSON, which is fine by me as it’s simple to work with in PHP! This example just decodes the result into an object and then we print_r() it*. The result is:
stdClass Object
(
[status_code] => 200
[status_txt] => OK
[data] => stdClass Object
(
[long_url] => http://lornajane.net/
[url] => http://bit.ly/nMGNp3
[hash] => nMGNp3
[global_hash] => glZgTN
[new_hash] => 1
)
)
The API also provides methods to get the long URL from a short one, validate a short one, and also get all sorts of statistics about a URL that you’ve shortened using bit.ly. I’m increasingly using bit.ly for shortening URLs, and also for their awesome bundles feature where you can give one link to refer to a list of resources. This comes in really handy for example when I’m giving talks, I can just refer everyone to one link to find everything that I mentioned! Using the API for these kinds of sites mean that we can integrate with systems that already talk to bit.ly, and also avoids us re-inventing a similar service. There are other ways to do this from PHP but since curl is a core extension, this should work on pretty much all installs.
* This is my personal blog, I can use function names as verbs here if I want to
Instead of using cURL, you can use file_get_contents() function to achieve the same thing.
[geshi lang=php]$url = “http://api.bitly.com/v3/shorten?login=username&apiKey=R_secret&longUrl=http%3A%2F%2Flornajane.net”;
$result = json_decode(file_get_contents($url));
print_r($result);
[/geshi]
Using file_get_contents is not reliable!
The file_get_contents (actually any of the stream handling functions) is another great way to deal with web requests from PHP, but relies on the allow_url_fopen setting being enabled in PHP. I think that’s what @mob means here?
There’s a PEAR package that does that too ( http://pear.php.net/package/Services_ShortURL/ ) better again it works with other shortener services as well as bitly. Admittedly the documentation needs a bit of work!
Last year I needed this functionality in a Zend Framework project so I wrote some code to do this using Zend_Http_Client.
Check it out: http://www.jeremyglover.com/blog/2010/08/26/shorten-urls-with-zend-framework-and-bit-ly/
Ken: I had no idea there was a PEAR package for this, that’s cool but this call was so trivial that I could call it faster than I could find out there was a library I think!
Jeremy: Ah, nice! Thanks for sharing :)
I have never seen a article like this before. Very interesting. Thanks
You forget to call “curl_close($ch);” at the end ;-)
http%3A%2F%2Fwww.Google.com
stdClass Object ( [data] => Array ( ) [status_code] => 500 [status_txt] => INVALID_URI )
using both old version (2011) and new version (2012) of your posts.
Bit.ly have updated their services, as I realised when I tried to integrate against them again earlier today – have a look at their new docs http://dev.bitly.com/api.html and the example that I’ll be adding to this site when I finish writing the words to accompany it – probably early next week!
Did you ever get the new version to work?
Useful, thanks. I’ve been coding web stuff for a long time, (since the early 90’s) but I fell off my trolly about that a couple of years ago, things went too fast, too many technologies making things too simple/too hard. Now it’s all a bit arcane again, and snippets like this make it handy when I’m trying to put something together. I feel comfortable again. Cheers.
Pingback: Programowanie w PHP » Blog Archive » Lorna Mitchell’s Blog: