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

1,136 responses to “Using the BitLy API to shorten urls”

  1. DanielTus Avatar
    DanielTus

    п»їJust now, I discovered a great guide regarding cheap Indian generics. It explains WHO-GMP protocols when buying antibiotics. If anyone wants factory prices, go here: п»їhttps://kisawyer.us.com/# mail order pharmacy india. Cheers.

  2. Ralphter Avatar
    Ralphter

    п»їActually, I stumbled upon a great report concerning ordering meds from India. It covers CDSCO regulations on prescriptions. In case you need factory prices, go here: п»їkisawyer.us.com. Worth a read.

  3. Richardobepe Avatar
    Richardobepe

    For those looking to save big on pills, I suggest reading this archive. It shows trusted Mexican pharmacies. Best prices found here: п»їUpstate Medical.

  4. Ralphter Avatar
    Ralphter

    п»їActually, I stumbled upon a great page regarding ordering meds from India. It details CDSCO regulations for generic meds. If anyone wants reliable shipping to USA, go here: п»їkisawyer.us.com. Good info.

  5. DanielTus Avatar
    DanielTus

    п»їLately, I stumbled upon a helpful guide about buying affordable antibiotics. It explains the manufacturing standards on prescriptions. If you are looking for Trusted Indian sources, read this: п»їbuy medicines online in india. Worth a read.

  6. Richardobepe Avatar
    Richardobepe

    I was wondering about safe Mexican pharmacies. I ran into a verified post that lists affordable options: п»їpolkcity.us.com. Any thoughts?.

  7. Lloydpenda Avatar
    Lloydpenda

    FYI, a detailed analysis on cross-border shipping rules. It explains pricing differences for ED meds. Full info: п»їUpstate Medical.

  8. Ralphter Avatar
    Ralphter

    п»їRecently, I stumbled upon an interesting page regarding ordering meds from India. It details the manufacturing standards when buying antibiotics. If anyone wants factory prices, take a look: п»їbuy medicines online in india. Might be useful.

  9. DanielTus Avatar
    DanielTus

    п»їTo be honest, I found an interesting guide about buying affordable antibiotics. The site discusses CDSCO regulations on prescriptions. If you are looking for factory prices, go here: п»їpharmacy website india. Cheers.

  10. Richardobepe Avatar
    Richardobepe

    I was wondering about getting antibiotics without prescription. I found a good archive that reviews affordable options: п»їpharmacy in mexico that ships to us. Any thoughts?.

  11. Ralphter Avatar
    Ralphter

    п»їLately, I came across an informative resource concerning ordering meds from India. It covers how to save money when buying antibiotics. In case you need Trusted Indian sources, read this: п»їhttps://kisawyer.us.com/# п»їlegitimate online pharmacies india. Hope it helps.

  12. Richardobepe Avatar
    Richardobepe

    Has anybody tried getting antibiotics without prescription. I saw a good site that compares affordable options: п»їhttps://polkcity.us.com/# mexico online pharmacy. What do you think?.

  13. Ralphter Avatar
    Ralphter

    п»їJust now, I came across a great guide regarding ordering meds from India. It covers WHO-GMP protocols for generic meds. In case you need cheaper alternatives, take a look: п»їп»їclick here. It helped me.

  14. madeinna.org Avatar

    us online casinos paypal

    References:
    madeinna.org

  15. https://jobsyt.com/ Avatar

    online casinos mit paypal

    References:
    https://jobsyt.com/

  16. Richardobepe Avatar
    Richardobepe

    If you want to save cash on meds, you should try visiting this report. It shows trusted Mexican pharmacies. Good deals found here: п»їUpstate Medical.

  17. Lloydpenda Avatar
    Lloydpenda

    If you want to save on pharmacy costs, I recommend visiting this archive. It shows prices for generics. Huge savings available here: п»їhttps://polkcity.us.com/# purple pharmacy online.

  18. Ralphter Avatar
    Ralphter

    п»їJust now, I came across a helpful article about Indian Pharmacy exports. It explains the manufacturing standards on prescriptions. If anyone wants cheaper alternatives, read this: п»їwebsite. Worth a read.

  19. slprofessionalcaregivers.lk Avatar

    australian online casinos that accept paypal

    References:
    slprofessionalcaregivers.lk

  20. DanielTus Avatar
    DanielTus

    п»їJust now, I found a useful report concerning generic pills from India. It explains CDSCO regulations on prescriptions. If anyone wants factory prices, go here: п»їthis link. It helped me.

  21. Richardobepe Avatar
    Richardobepe

    Stop overpaying and save cash on meds, you should try checking this page. It reveals shipping costs. Good deals found here: п»їhttps://polkcity.us.com/# mexican online pharmacy wegovy.

  22. Ralphter Avatar
    Ralphter

    п»їLately, I stumbled upon a helpful page regarding cheap Indian generics. It explains CDSCO regulations on prescriptions. In case you need reliable shipping to USA, check this out: п»їuseful link. It helped me.

  23. DanielTus Avatar
    DanielTus

    п»їLately, I discovered a helpful resource about buying affordable antibiotics. It details WHO-GMP protocols for generic meds. If anyone wants factory prices, go here: п»їkisawyer.us.com. Cheers.

  24. Richardobepe Avatar
    Richardobepe

    п»їJust now, I discovered an informative article about generic pills availability. It explains regulations on prescriptions. If you are looking for affordable options, read this: п»їhttps://polkcity.us.com/# medicine from mexico. Cheers.

  25. Lloydpenda Avatar
    Lloydpenda

    п»їLately, I discovered an informative report about Mexican Pharmacy standards. It covers FDA equivalents when buying antibiotics. If you are looking for reliable shipping to USA, take a look: п»їpolkcity.us.com. Hope it helps.

  26. Ralphter Avatar
    Ralphter

    п»їJust now, I stumbled upon an informative page concerning Indian Pharmacy exports. The site discusses the manufacturing standards when buying antibiotics. If you are looking for factory prices, go here: п»їfollow. Cheers.

  27. DanielTus Avatar
    DanielTus

    п»їRecently, I stumbled upon a useful guide concerning Indian Pharmacy exports. The site discusses the manufacturing standards for ED medication. If anyone wants factory prices, check this out: п»їkisawyer.us.com. Hope it helps.

  28. Richardobepe Avatar
    Richardobepe

    п»їActually, I found an interesting article about generic pills availability. It covers how to save money on prescriptions. If anyone wants Trusted pharmacy sources, visit this link: п»їuseful link. Hope it helps.

  29. Ralphter Avatar
    Ralphter

    п»їLately, I stumbled upon a great resource about generic pills from India. It details WHO-GMP protocols on prescriptions. If anyone wants factory prices, visit this link: п»їtop 10 pharmacies in india. Might be useful.

  30. DanielTus Avatar
    DanielTus

    п»їTo be honest, I came across an informative resource regarding generic pills from India. The site discusses CDSCO regulations for generic meds. If you are looking for cheaper alternatives, visit this link: п»їOnline medicine order. Might be useful.

  31. Richardobepe Avatar
    Richardobepe

    Has anybody tried getting antibiotics without prescription. I saw a good site that reviews affordable options: п»їhttps://polkcity.us.com/# mexican pharmacies that ship to the united states. Any thoughts?.

  32. Ralphter Avatar
    Ralphter

    п»їJust now, I stumbled upon an informative resource regarding ordering meds from India. The site discusses WHO-GMP protocols on prescriptions. If you are looking for cheaper alternatives, take a look: п»їkisawyer.us.com. Worth a read.

  33. DanielTus Avatar
    DanielTus

    п»їActually, I found a helpful article regarding cheap Indian generics. It details the manufacturing standards for ED medication. If you are looking for Trusted Indian sources, check this out: п»їkisawyer.us.com. Hope it helps.

  34. Lloydpenda Avatar
    Lloydpenda

    Just wanted to share, an important analysis on FDA equivalent standards. It explains pricing differences for antibiotics. Source: п»їpolkcity.us.com.

  35. Richardobepe Avatar
    Richardobepe

    To save on pharmacy costs, I suggest visiting this page. The site explains where to buy cheap. Best prices at this link: п»їpolkcity.us.com.

  36. DanielTus Avatar
    DanielTus

    п»їRecently, I discovered an interesting guide about ordering meds from India. The site discusses the manufacturing standards for ED medication. If you are looking for cheaper alternatives, take a look: п»їhttps://kisawyer.us.com/# india online pharmacy. Worth a read.

  37. Ralphter Avatar
    Ralphter

    п»їActually, I stumbled upon an interesting page about Indian Pharmacy exports. It details how to save money for generic meds. For those interested in reliable shipping to USA, go here: п»їhere. Hope it helps.

  38. Richardobepe Avatar
    Richardobepe

    For those looking to save big on pills, you should try reading this resource. The site explains where to buy cheap. Best prices found here: п»їview.

  39. DanielTus Avatar
    DanielTus

    п»їJust now, I stumbled upon a useful guide regarding buying affordable antibiotics. The site discusses WHO-GMP protocols on prescriptions. For those interested in Trusted Indian sources, go here: п»їhttps://kisawyer.us.com/# cheapest online pharmacy india. It helped me.

  40. Ralphter Avatar
    Ralphter

    п»їJust now, I discovered a great resource concerning generic pills from India. The site discusses CDSCO regulations on prescriptions. In case you need Trusted Indian sources, go here: п»їkisawyer.us.com. Hope it helps.

  41. Lloydpenda Avatar
    Lloydpenda

    FYI, a detailed analysis on cross-border shipping rules. It explains how to avoid scams for generics. Full info: п»їmexican pharmacies.

  42. Richardobepe Avatar
    Richardobepe

    If you want to save money on prescriptions, you should try reading this page. It shows shipping costs. Huge savings available here: п»їhttps://polkcity.us.com/# mexico medication.

  43. DanielTus Avatar
    DanielTus

    п»їTo be honest, I came across a great guide about ordering meds from India. It details how to save money on prescriptions. For those interested in Trusted Indian sources, go here: п»їhttps://kisawyer.us.com/# reputable indian pharmacies. It helped me.

  44. Ralphter Avatar
    Ralphter

    п»їTo be honest, I stumbled upon a great page regarding cheap Indian generics. It details CDSCO regulations for generic meds. If you are looking for Trusted Indian sources, check this out: п»їindian pharmacies safe. It helped me.

  45. DanielTus Avatar
    DanielTus

    п»їRecently, I found a great report about cheap Indian generics. It explains WHO-GMP protocols for generic meds. In case you need reliable shipping to USA, take a look: п»їhttps://kisawyer.us.com/# india pharmacy mail order. Good info.

Leave a Reply

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