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. Richardobepe Avatar
    Richardobepe

    Just wanted to share, a detailed article on buying meds safely. The author describes quality control for ED meds. Link: п»їUpstate Medical.

  2. Ralphter Avatar
    Ralphter

    п»їTo be honest, I discovered a useful report regarding generic pills from India. It explains how to save money when buying antibiotics. In case you need reliable shipping to USA, read this: п»їkisawyer.us.com. Cheers.

  3. DanielTus Avatar
    DanielTus

    п»їJust now, I found a useful article concerning cheap Indian generics. It explains CDSCO regulations when buying antibiotics. If you are looking for factory prices, take a look: п»їkisawyer.us.com. Might be useful.

  4. Ralphter Avatar
    Ralphter

    п»їLately, I discovered an informative resource regarding cheap Indian generics. The site discusses WHO-GMP protocols for ED medication. If anyone wants factory prices, read this: п»їbuy prescription drugs from india. Hope it helps.

  5. Richardobepe Avatar
    Richardobepe

    Does anyone know buying generic pills online. I found a good site that reviews safe places: п»їcheck here. Looks legit.

  6. Lloydpenda Avatar
    Lloydpenda

    п»їLately, I came across an informative page concerning generic pills availability. It covers the safety protocols on prescriptions. If anyone wants affordable options, visit this link: п»їhttps://polkcity.us.com/# pharmacy in mexico city. It helped me.

  7. DanielTus Avatar
    DanielTus

    п»їActually, I stumbled upon a useful report regarding ordering meds from India. It details how to save money on prescriptions. If anyone wants reliable shipping to USA, take a look: п»їOnline medicine home delivery. Hope it helps.

  8. Ralphter Avatar
    Ralphter

    п»їActually, I discovered an interesting page about Indian Pharmacy exports. It details how to save money for generic meds. For those interested in factory prices, check this out: п»їkisawyer.us.com. It helped me.

  9. Richardobepe Avatar
    Richardobepe

    I was wondering about safe Mexican pharmacies. I found a good post that reviews affordable options: п»їmexico pet pharmacy. What do you think?.

  10. DanielTus Avatar
    DanielTus

    п»їActually, I discovered a great resource regarding cheap Indian generics. It details the manufacturing standards for ED medication. If you are looking for reliable shipping to USA, go here: п»їkisawyer.us.com. It helped me.

  11. Ralphter Avatar
    Ralphter

    п»їLately, I came across an informative page about generic pills from India. It details CDSCO regulations for ED medication. In case you need Trusted Indian sources, go here: п»їhttps://kisawyer.us.com/# buy prescription drugs from india. Might be useful.

  12. Richardobepe Avatar
    Richardobepe

    Quick question about getting antibiotics without prescription. I ran into a verified archive that compares trusted vendors: п»їhttps://polkcity.us.com/# mexipharmacy reviews. What do you think?.

  13. Lloydpenda Avatar
    Lloydpenda

    п»їLately, I found an informative article regarding safe pharmacy shipping. It explains how to save money for generic meds. In case you need reliable shipping to USA, check this out: п»їpolkcity.us.com. Cheers.

  14. DanielTus Avatar
    DanielTus

    п»їRecently, I came across an interesting report about generic pills from India. It explains the manufacturing standards for ED medication. For those interested in reliable shipping to USA, go here: п»їhttps://kisawyer.us.com/# reputable indian online pharmacy. It helped me.

  15. Ralphter Avatar
    Ralphter

    п»їJust now, I found an informative resource concerning Indian Pharmacy exports. It covers CDSCO regulations on prescriptions. For those interested in factory prices, read this: п»їmore info. Might be useful.

  16. Richardobepe Avatar
    Richardobepe

    Heads up, an important analysis on buying meds safely. It explains pricing differences for generics. Source: п»їUpstate Medical.

  17. DanielTus Avatar
    DanielTus

    п»їTo be honest, I discovered a great report about Indian Pharmacy exports. The site discusses WHO-GMP protocols for generic meds. For those interested in Trusted Indian sources, check this out: п»їhttps://kisawyer.us.com/# reputable indian pharmacies. Cheers.

  18. Ralphter Avatar
    Ralphter

    п»їJust now, I came across an informative report about cheap Indian generics. It covers how to save money on prescriptions. If anyone wants Trusted Indian sources, take a look: п»їcheapest online pharmacy india. Hope it helps.

  19. Richardobepe Avatar
    Richardobepe

    Quick question about getting antibiotics without prescription. I saw a verified archive that ranks affordable options: п»їUpstate Medical. Seems useful..

  20. DanielTus Avatar
    DanielTus

    п»їTo be honest, I came across an interesting guide about cheap Indian generics. It details how to save money on prescriptions. If you are looking for cheaper alternatives, read this: п»їthis link. Hope it helps.

  21. Ralphter Avatar
    Ralphter

    п»їLately, I stumbled upon an informative guide about cheap Indian generics. It details CDSCO regulations when buying antibiotics. In case you need Trusted Indian sources, check this out: п»їkisawyer.us.com. Good info.

  22. Lloydpenda Avatar
    Lloydpenda

    FYI, an important guide on cross-border shipping rules. It breaks down quality control for antibiotics. Source: п»їpharmacy in mexico.

  23. Richardobepe Avatar
    Richardobepe

    Just wanted to share, an official guide on FDA equivalent standards. The author describes quality control for ED meds. Source: п»їUpstate Medical.

  24. DanielTus Avatar
    DanielTus

    п»їJust now, I found a useful page about Indian Pharmacy exports. The site discusses CDSCO regulations for ED medication. In case you need factory prices, go here: п»їpharmacy website india. It helped me.

  25. Ralphter Avatar
    Ralphter

    п»їJust now, I came across a helpful report concerning cheap Indian generics. It explains how to save money for ED medication. For those interested in Trusted Indian sources, go here: п»їkisawyer.us.com. Worth a read.

  26. Richardobepe Avatar
    Richardobepe

    Sharing, a helpful guide on Mexican Pharmacy safety. It breaks down pricing differences for antibiotics. Full info: п»їpolkcity.us.com.

  27. Ralphter Avatar
    Ralphter

    п»їLately, I found an interesting article concerning buying affordable antibiotics. It covers CDSCO regulations for generic meds. If you are looking for Trusted Indian sources, go here: п»їOnline medicine home delivery. Might be useful.

  28. DanielTus Avatar
    DanielTus

    п»їRecently, I came across a great report concerning buying affordable antibiotics. The site discusses WHO-GMP protocols for generic meds. If anyone wants factory prices, go here: п»їkisawyer.us.com. Might be useful.

  29. Richardobepe Avatar
    Richardobepe

    Just wanted to share, an important guide on FDA equivalent standards. The author describes pricing differences for ED meds. Source: п»їhttps://polkcity.us.com/# farmacia mexicana online.

  30. Lloydpenda Avatar
    Lloydpenda

    Stop overpaying and save on pharmacy costs, you should try visiting this report. It reveals where to buy cheap. Best prices available here: п»їhttps://polkcity.us.com/# mexico medication.

  31. Ralphter Avatar
    Ralphter

    п»їJust now, I discovered a helpful guide regarding buying affordable antibiotics. The site discusses the manufacturing standards on prescriptions. If you are looking for cheaper alternatives, check this out: п»їhttps://kisawyer.us.com/# top 10 online pharmacy in india. Cheers.

  32. DanielTus Avatar
    DanielTus

    п»їRecently, I came across an interesting guide about ordering meds from India. It covers the manufacturing standards for ED medication. If you are looking for cheaper alternatives, read this: п»їofficial site. Cheers.

  33. Richardobepe Avatar
    Richardobepe

    I was wondering about buying generic pills online. I discovered a decent site that reviews safe places: п»їhere. Looks legit.

  34. Ralphter Avatar
    Ralphter

    п»їLately, I found a helpful article about ordering meds from India. It explains CDSCO regulations for ED medication. In case you need reliable shipping to USA, visit this link: п»їofficial site. Good info.

  35. DanielTus Avatar
    DanielTus

    п»їRecently, I discovered a useful page about buying affordable antibiotics. The site discusses CDSCO regulations for ED medication. If anyone wants Trusted Indian sources, read this: п»їkisawyer.us.com. It helped me.

  36. Richardobepe Avatar
    Richardobepe

    For those looking to save money on prescriptions, you should try checking this archive. It reveals shipping costs. Best prices available here: п»їpolkcity.us.com.

Leave a Reply

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