Using the BitLy API to shorten urls

I was asked to place Twitter messages with product links when new products were added to a webshop. I’ve used my own Twitter class for posting the first test Tweet then I went looking for a shorten url a service to shorten the product links.

There are many url shorten services available on the internet, one of these services is Bitly.
The reasons why I’ve chosen Bitly:
– Urls don’t expire after a period of time.
– Bitly provides a click url summary (although Google Analytics can be used for this instead)
– Urls are short in length so i could use the remaining Tweet length for a small product description.
– And more important, they provide an easy to use API.
– Authentication does not require a password, we can use our API key instead.

Before we can connect to the API, we need a Bitly account, the account can be created at http://bit.ly/ to obtain a free API key.

The communication process is quite simple:
– Authenticate with your Bitly username and API key
– Call the right Bitly API url with the correct parameters for the action we want to perform
– Process the result

Below are some snippets of my Bitly class:

class BitLy
{
	/**
	 * Version of the bit ly api
	 * @var string
	 */
	private $sApiVersion = '2.0.1';

	/**
	 * Url of the api
	 * @var string
	 */
	private $sApiUrl = 'http://api.bit.ly/';

	/**
	 * username
	 * @var string
	 */
	private $sUsername;

	/**
	 * apikey
	 * @var string
	 */
	private $sApiKey;

	/**
	 * Set username and apikey
	 *
	 * @param string $sUsername
	 * @param string $sApiKey
	 * @return void
	 */
	public function  __construct($sUsername, $sApiKey) {
		$this->setUsername($sUsername);
		$this->setApiKey($sApiKey);
	}

	...

	/**
	 * shorten
	 *
	 * @param string $sLongUrl
	 * @return object with shortend url and hash
	 */
	public function shorten($sLongUrl)
	{
		return $this->process('shorten', urlencode($sLongUrl));
	}

	/**
	 * expand
	 *
	 * @param string $sShortUrl
	 * @return object with original url
	 */
	public function expand($sShortUrl)
	{
		return $this->process('expand', urlencode($sShortUrl));
	}

	...

	/**
	 * process, checks the action and returns the result
	 *
	 * @param string $sAction
	 * @param string $sParam
	 */
	private function process($sAction, $sUrlToParse)
	{
		// Check the url, if no http is present, add it
		if(strstr($sUrlToParse, 'http://') === false) {
			$sUrlToParse = 'http://'.$sUrlToParse;
		}

		// Compose action url
		$sParam = ($sAction == 'shorten') ? 'longUrl='.$sUrlToParse : 'shortUrl='.$sUrlToParse;
		$sActionUrl = $this->sApiUrl.$sAction.'?version='.$this->sApiVersion.'&'.$sParam.'&login='.$this->sUsername.'&apiKey='.$this->sApiKey.'&format=xml';

		// Get data
		libxml_use_internal_errors(true);
		$oData = simplexml_load_file($sActionUrl);
		if(!$oData) {
			throw new Exception('Api returned an invalid response!');
		}

		// Check for errors
		if($oData->statusCode == 'ERROR') {
			throw new Exception($oData->errorCode.': '.$oData->errorMessage);
		}

		// Build result object
		$oResult				= new stdClass();
		$oResult->requestedUrl	= $sUrlToParse;
		$oResult->resultUrl	= (string)$oData->results->nodeKeyVal->shortUrl;
		$oResult->hash		= (string)$oData->results->nodeKeyVal->hash;

		return $oResult;
	}

The reason why I build a result object is because of the fact that the server where the class is deployed doesn’t support JSON but JSON is also available as return format.

Furthermore, generated urls can be saved to our Bitly account history by adding an additional parameter to the url (history = 1). Another nice option of the API is to shorten the URL to a keyword, this can be achieved by sending the (keyword=your keyword) parameter along.

The complete API manual can be found at Google Code which also describes how to get click statistics of the shortened links.

Comments

972 responses to “Using the BitLy API to shorten urls”

  1. JoshuaMiz Avatar
    JoshuaMiz

    mexican drugstore online https://muscpharm.xyz/# Musc Pharm

  2. DmuSaT Avatar

    Cialis without a doctor prescription: Dmu Cialis – DmuCialis

  3. RamonJox Avatar
    RamonJox

    Kamagra Oral Jelly super kamagra NeoKamagra

  4. CialisHaugh Avatar

    cialis for sale: DmuCialis – Cheap Cialis

  5. JoshuaMiz Avatar
    JoshuaMiz

    mail order drug store https://dmucialis.xyz/# Buy Tadalafil 5mg

  6. RamonJox Avatar
    RamonJox

    Neo Kamagra Kamagra 100mg price Neo Kamagra

  7. JoshuaMiz Avatar
    JoshuaMiz

    express pharmacy https://muscpharm.xyz/# Musc Pharm

  8. CialisHaugh Avatar

    Neo Kamagra: NeoKamagra – Neo Kamagra

  9. DmuSaT Avatar

    Neo Kamagra: NeoKamagra – Kamagra 100mg price

  10. RamonJox Avatar
    RamonJox

    Dmu Cialis DmuCialis DmuCialis

  11. JoshuaMiz Avatar
    JoshuaMiz

    mail order canadian drugs http://dmucialis.com/# Cialis without a doctor prescription

  12. CialisHaugh Avatar

    Buy Tadalafil 10mg: Dmu Cialis – Dmu Cialis

  13. RamonJox Avatar
    RamonJox

    Cialis without a doctor prescription Buy Tadalafil 20mg Dmu Cialis

  14. JoshuaMiz Avatar
    JoshuaMiz

    online pharmacies https://neokamagra.xyz/# Neo Kamagra

  15. DmuSaT Avatar

    Dmu Cialis: Dmu Cialis – DmuCialis

  16. CialisHaugh Avatar

    Neo Kamagra: Neo Kamagra – Neo Kamagra

  17. JoshuaMiz Avatar
    JoshuaMiz

    canada pharmaceuticals online https://dmucialis.xyz/# DmuCialis

  18. RamonJox Avatar
    RamonJox

    Neo Kamagra Neo Kamagra NeoKamagra

  19. CialisHaugh Avatar

    MuscPharm: MuscPharm – MuscPharm

  20. JoshuaMiz Avatar
    JoshuaMiz

    legitimate canadian mail order pharmacies https://muscpharm.com/# online medications

  21. RamonJox Avatar
    RamonJox

    Dmu Cialis DmuCialis Generic Tadalafil 20mg price

  22. CialisHaugh Avatar

    MuscPharm: Musc Pharm – prescription drugs without prescription

  23. JoshuaMiz Avatar
    JoshuaMiz

    cheapest canadian pharmacies https://muscpharm.xyz/# MuscPharm

  24. RamonJox Avatar
    RamonJox

    canadian pharmacy selling viagra top rated canadian pharmacies online Musc Pharm

  25. CialisHaugh Avatar

    Kamagra Oral Jelly: Neo Kamagra – NeoKamagra

  26. JoshuaMiz Avatar
    JoshuaMiz

    medicin without prescription https://dmucialis.com/# DmuCialis

  27. RamonJox Avatar
    RamonJox

    Kamagra Oral Jelly Kamagra 100mg price Kamagra 100mg

  28. CialisHaugh Avatar

    vipps accredited online pharmacy: canada drug – MuscPharm

  29. JoshuaMiz Avatar
    JoshuaMiz

    order prescription medicine online without prescription https://neokamagra.com/# NeoKamagra

  30. RamonJox Avatar
    RamonJox

    Musc Pharm Musc Pharm pharmacy online canada

  31. CialisHaugh Avatar

    MuscPharm: canadian pharmaceuticals for usa sales – trustworthy canadian pharmacy

  32. JoshuaMiz Avatar
    JoshuaMiz

    top canadian pharmacies http://dmucialis.com/# Generic Cialis without a doctor prescription

  33. RamonJox Avatar
    RamonJox

    Neo Kamagra super kamagra Kamagra 100mg

  34. CialisHaugh Avatar

    Dmu Cialis: DmuCialis – Dmu Cialis

  35. JoshuaMiz Avatar
    JoshuaMiz

    mexican drug pharmacy https://dmucialis.com/# DmuCialis

  36. RamonJox Avatar
    RamonJox

    canadian pharmacies online legitimate MuscPharm MuscPharm

  37. CialisHaugh Avatar

    Kamagra 100mg price: п»їkamagra – Neo Kamagra

  38. JoshuaMiz Avatar
    JoshuaMiz

    safe canadian pharmacy https://muscpharm.xyz/# canadian pharmaceuticals online safe

  39. DmuSaT Avatar

    most trusted canadian online pharmacy: MuscPharm – MuscPharm

  40. CialisHaugh Avatar

    Musc Pharm: Musc Pharm – Musc Pharm

  41. JoshuaMiz Avatar
    JoshuaMiz

    best mail order canadian pharmacy http://dmucialis.com/# Dmu Cialis

  42. CialisHaugh Avatar

    Dmu Cialis: DmuCialis – Dmu Cialis

  43. JoshuaMiz Avatar
    JoshuaMiz

    meds online without doctor prescription http://dmucialis.com/# DmuCialis

Leave a Reply

Your email address will not be published. Required fields are marked *