New Bit.ly Class to use with CakePHP or by it self

In: Tutorials

6 Aug 2009

If you use service like Twitter where you only get a limited number of characters to post or if you are just sick of dealing with unruly long urls you might be familliar with url shortening services such as Bit.ly.

I personally like them because of they offer a nice API and statistics all for free. It is a pretty quick service too. You can generate hundreds of URLs in just a few seconds. All you have to do is register and you get a username and API key which you will need in the following section.

Here is the code … As you can see I only implemented the shorten for now but I will probably add some of the other stuff later. For some reason WP won’t let me post curl_exec( so be sure to remove the extra character.

class bitly {
	/**
	 * A PHP class to utilize the bitly API
	 * through the Bitly API. Handles:
	 * - Shorten
	 *
	 * Requires PHP5, CURL, JSON and a Bitly account http://bit.ly/
	 *
	 * Bitly API documentation: http://code.google.com/p/bitly-api/wiki/ApiDocumentation
	 */
 
	protected $appkey;
	protected $login;
	protected $version;
 
	/**
	* Contruct the Bitly Class
	*
    * @param string, your Bitly account key
    * @param string, your Bitly account login
	*/
	public function __construct($login, $appkey, $version='2.0.1') {
		$this->login = $login;
		$this->appkey = $appkey;
		$this->version = $version;
	}
 
	/**
	* Retrieve a shortened URL
	*
    * @param string, url to shorten
    * @param string, version of Bitly to use
	*/
	public function shorten($url) {
		//create the URL
		$api_url = 'http://api.bit.ly/shorten?version='.$this->version.'&longUrl='.urlencode($url).'&format=xml&login='.$this->login.'&apiKey='.$this->appkey;
		//call the API
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $api_url);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$response = curl_ex_ec($curl);
		curl_close($curl);
 
		//parse the XML response and return the url
		$xml_object = new SimpleXMLElement($response);
		return $xml_object->results->nodeKeyVal->shortUrl;
	}
 
}

If you want to use this in CakePHP … you will want to do something like this.
Put this line before the class … I actually use this in a helper since this is a view level class

App::import('Vendor','Bitly', array("file" => "bitly/bitly.class.php"));

Now you can just invoke it in one of your functions somewhere with two simple calls!

$bitly = new Bitly('username', 'your_API_key_here');
$short_url = $bitly->shorten($long_url);

Simple as that … oh and one more thing. Be sure to have CURL installed else this won’t work.

1 Response to New Bit.ly Class to use with CakePHP or by it self

Avatar

Simon

October 19th, 2009 at 3:32 am

use

$response = curl_exec($curl);

instead of

$response = curl_ex_ec($curl);

Regards Simon

Comment Form

SoliTech GmbH

Services such as Facebook, MySpace, LinkedIn, Twitter etc. are experiencing exponential growth. Your company needs to stay competitive and utilize this new technology. We will help you devise and execute a higly effective marketing strategy that will grow your brands' following and deliver measurable results.

Click here to see all our service.

What’s new in the forums?

  • New idea for the Toplist :D July 31, 2010
    OK, i was thinking about something... A top list inside of a clan. Im not sure where it would go though, maybe another tab with the clan name, view... […]
  • Add clan invites July 21, 2010
    I think we should bring back the option of add ninja to clan that way we can send invites to people. […]
  • Mass leveling system needs fixing July 20, 2010
    I know the mass leveling system has gotten to be a huge part of a ninja's life now, with the exp coming to us so easily, and the leveling coming just... […]
  • Transferring, fixed. July 20, 2010
    The transfer system greatly unbalanced the game, especially near the end (yet again im sorry for that), but would be it be possible to have it come... […]
  • Useless Clans July 16, 2010
    Clone Troopers Size: 0 Clone Troopers Size: 0 The Sereitei Size: 0 Empire […]
  • An item?...From a Ninja? July 16, 2010
    The title may confuse you on what i really mean, but i was thinking, is there a possibility that you could obtain items from killing other ninjas? it... […]
  • DP July 16, 2010
    I have been playing DuelingNinjas for a long time....and i still dont understand how the DP of armors are taken away. I can have 2bil health, yet... […]
  • Commas??? July 15, 2010
    I think it would be a lot easyer for ninjas these days if the levels, xp, yen, and hp had commas to divide the hundreds thousands and millions.... […]