postimg
Dec 2009 24

URL Shortening, for the Faint of Heart0

Posted In Miscellaneous By John Hass

Hello, Hello, Hello. Quick tut on bit.ly in PHP, nothing more nothing less (and yes I said PHP not Perl like I am so fond of). In order to use this code you need a bit.ly account. Visit http://bit.ly/account/register?rd=/ to get a login and a API Key.

Here is the function:

<?php
function shortenURL($url) {
$login = "yourlogin";
$apikey = "your api key";
 
$newurl = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl=".$url."&login=".$login."&apiKey=".$apikey);
$content = json_decode($newurl,true);
$error = $content["errorCode"];
if ($error == 0){
$newurl = $content["results"][$url]["shortUrl"];
}else {
$newurl = 0;
}
 
return $newurl;
}
?>

Just include this file in any script and call

and boom it’s shortened.

Merry Christmas you filthy animal.

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • email
  • LinkedIn
  • MySpace
  • PDF
  • Reddit
  • RSS
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes