Posted by brant : 2009-04-02 at 1:49 am

As promised here is some Bit.ly PHP example code using their API to generate URLs.  There is also some extra code that deals with advanced API usage towards the end of this post.  It is important to note that these examples assume that you have cURL installed.  I am also opting to use the JSON method of data delivery.  Now let's get to the Bit.ly basics and look at a function to generate a shortened URL.

/* Example code */
$link = "http://www.x-pose.org";

print getSmallLink($link);

function getSmallLink($longurl){
// Bit.ly
$url = "http://api.bit.ly/shorten?version=2.0.1&longUrl=$longurl&login=YOURLOGIN&apiKey=YOURAPIKEY&format=json&history=1";

$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
curl_setopt($s,CURLOPT_HEADER,false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($s);
curl_close( $s );

$obj = json_decode($result, true);
return $obj["results"]["$longurl"]["shortUrl"];
}

If you want to get fancy then you can add error checking.  Also the variable &history=1 in the URL allows you to keep track of your generated urls via the API.  Otherwise they are not saved in your history.  Now lets get to some advanced Bit.ly usage. . 

Let's say you are trying to grab some extra data from the following Bit.ly link: http://bit.ly/info/13SH5 .  Of course the most important part of that link is the Hash at the end. Here is an example how to print the Title of the page and the meta description:
/* Example code */
$hash = '13SH5';
$url = "http://api.bit.ly/info?version=2.0.1&login=YOURNAME&apiKey=YOURAPIKEY&hash=$hash";

$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url);
curl_setopt($s,CURLOPT_HEADER,false);
curl_setopt($s,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($s);
curl_close( $s );

$obj = json_decode($result, true);
print $obj["results"]["$hash"]["htmlTitle"];
print $obj["results"]["$hash"]["htmlMetaDescription"];
Still with me?  As you know there is much more data than just the title and description for some of the info pages.  Some even include automatically generated categories of what the webpage is about.  So lets save those categories into an array that were tagged for this page:

/* Example code */
foreach($obj["results"]["$hash"]["calais"] as $items){
			$cat[] = $items[0];
}
As you can see Bit.ly is very powerfull stuff.  Hopefuly this example code was able to help you out.  Stay tuned for my next batch of useful example code that allows you to automatically make blog posts via wordpress.



Similar Blogs:
Bit.ly Can Prevent And Detect Tiwtter Spam
Bit.ly Trumps TinyURL
TinyURL Sucks

blog comments powered by Disqus

OLD Comments (6)


Taylor Dewey - 2009-12-19 at 03:58:45
Perfect snipit of code. I'm using it in a WordPress install to help populate a "Tweet This" button -- Thanks!

oZestretch - 2009-07-18 at 09:07:00
@Scott Lewis
I not tried this, but with your URL's in API, try changing the "&" in the urls to "\&".

From: http://uloop.com?a=123&b=456&c=789&d=0
To: http://uloop.com?a=123\&b=456\&c=789\&d=0

brant - 2009-06-16 at 21:38:42
@Scott

It makes sense that the URL doesn't work because of the added variables. (The bit.ly url already has variables). I am sure this is talked about in the Bit.ly api forums, but my suggestion would be to use modrewrite.

From: http://uloop.com?a=123&b=456&c=789&d=0
To: http://uloop.com/123/456/789/0

It will definitely work that way. Still, I suggest you check out the Bit.ly forums as well.

Scott Lewis - 2009-06-16 at 02:01:22
I have multiple variables in my URL and bit.ly's api is rejecting them. The url is something like http://uloop.com?a=123&b=456&c=789&d=0. I'm able to paste the URL into the bit.ly site and it works successfully, but the API doesn't take it. Any idea what I need to do in order to get the multiple variable approach to work through the api?

dsfsdfds - 2009-04-22 at 16:33:09
super!

Jeff - 2009-04-02 at 12:44:16
Nice tips on Bit.ly. I had no idea they actually kept so much data about each shortened link.