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

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

  1. DouglasVox Avatar
    DouglasVox

    nagad88: current Nagad88 entry page – nagad88

  2. Fun88Daw Avatar

    Dabet Vietnam liên kết đang sử dụng: đường dẫn vào Dabet hiện tại – Dabet main access URL

  3. DouglasVox Avatar
    DouglasVox

    Fun88 Vietnam main access page: Fun88 Vietnam lien k?t truy c?p hi?n t?i – fun88

  4. DonaldImpaw Avatar
    DonaldImpaw

    https://nagad88.top/# Nagad88 Bangladesh official access

  5. Fun88Daw Avatar

    Nagad88 latest working link: nagad88 লগইন করুন – Nagad88 এ ঢোকার রেফারেন্স পেজ

  6. Bryanvat Avatar
    Bryanvat

    DarazPlay latest access address DarazPlay এ ঢোকার জন্য এখনকার লিংক working DarazPlay access page

  7. JuliusRam Avatar
    JuliusRam

    https://planbet.sbs/# PLANBET working address for Bangladesh

  8. DouglasVox Avatar
    DouglasVox

    PLANBET latest entry link: PLANBET ???? ???? ???? ??????? ???? – planbet

  9. DonaldImpaw Avatar
    DonaldImpaw

    https://fun88.sale/# link Fun88 Vietnam đang hoạt động

  10. Fun88Daw Avatar

    Fun88 Vietnam liên kết truy cập hiện tại: fun88 – Fun88 Vietnam liên kết truy cập hiện tại

  11. Bryanvat Avatar
    Bryanvat

    đường dẫn vào Dabet hiện tại Dabet Vietnam current access link Dabet Vietnam liên kết đang sử dụng

  12. DouglasVox Avatar
    DouglasVox

    Dabet Vietnam current access link: du?ng d?n vao Dabet hi?n t?i – dabet

  13. DonaldImpaw Avatar
    DonaldImpaw

    https://planbet.sbs/# PLANBET এ ঢোকার আপডেটেড লিংক

  14. Fun88Daw Avatar

    trang tham chiếu Fun88 Vietnam: trang tham chiếu Fun88 Vietnam – current Fun88 Vietnam URL

  15. Bryanvat Avatar
    Bryanvat

    Nagad88 আপডেটেড প্রবেশ ঠিকানা nagad88 login Nagad88 Bangladesh বর্তমান লিংক

  16. JuliusRam Avatar
    JuliusRam

    https://dabet.reviews/# d?a ch? truy c?p Dabet m?i nh?t

  17. DonaldImpaw Avatar
    DonaldImpaw

    https://darazplay.blog/# DarazPlay updated entry link

  18. DouglasVox Avatar
    DouglasVox

    PLANBET latest entry link: planbet login – planbet

  19. devkona.net Avatar

    online roulette play roulette and receive $1500

    References:
    https://git.sayndone.ru/adelaidetreloa

  20. Fun88Daw Avatar

    working DarazPlay access page: DarazPlay Bangladesh official link – working DarazPlay access page

  21. Bryanvat Avatar
    Bryanvat

    link Dabet hoạt động cho người dùng Việt Nam latest Dabet Vietnam link Dabet Vietnam liên kết đang sử dụng

  22. DonaldImpaw Avatar
    DonaldImpaw

    https://darazplay.blog/# DarazPlay বর্তমান প্রবেশ পথ

  23. DouglasVox Avatar
    DouglasVox

    lien k?t vao Fun88 cho ngu?i dung Vi?t Nam: Fun88 updated entry link – d?a ch? vao Fun88 m?i nh?t

Leave a Reply

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