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

3,124 responses to “Using the BitLy API to shorten urls”

  1. Edwardkeync Avatar
    Edwardkeync

    http://primeindiameds.com/# canada discount pharmacy

  2. PhillipStibe Avatar
    PhillipStibe

    https://northcarerx.shop/# canadian 24 hour pharmacy

  3. MatthewBom Avatar
    MatthewBom

    NorthCareRx: NorthCareRx – global pharmacy canada

  4. Stevewache Avatar
    Stevewache

    safe online pharmacies Prime India Meds legitimate canadian online pharmacies

  5. Edwardkeync Avatar
    Edwardkeync

    https://northcarerx.com/# canadian pharmacy review

  6. MatthewBom Avatar
    MatthewBom

    NorthCareRx: canadian pharmacy 365 – canada drugs online review

  7. Edwardkeync Avatar
    Edwardkeync

    https://bordercarerx.com/# legit mexican pharmacy without prescription

  8. MatthewBom Avatar
    MatthewBom

    best canadian online pharmacy reviews: NorthCareRx – NorthCareRx

  9. Stevewache Avatar
    Stevewache

    NorthCareRx NorthCareRx NorthCareRx

  10. Michaelvop Avatar
    Michaelvop

    canadian pharmacy 365: Prime India Meds – canada online pharmacy

  11. PhillipStibe Avatar
    PhillipStibe

    http://bordercarerx.com/# reputable pharmacies in mexico

  12. Edwardkeync Avatar
    Edwardkeync

    https://bordercarerx.com/# mexico pharmacies online

  13. MatthewBom Avatar
    MatthewBom

    BorderCareRX: legit mexican pharmacy without prescription – legit mexican pharmacy without prescription

  14. Michaelvop Avatar
    Michaelvop

    legal to buy prescription drugs from canada: NorthCareRx – canada pharmacy

  15. Edwardkeync Avatar
    Edwardkeync

    https://primeindiameds.shop/# canada online pharmacy

  16. MatthewBom Avatar
    MatthewBom

    what’s the best online pharmacy: top 10 pharmacies in india – canadian 24 hour pharmacy

  17. Stevewache Avatar
    Stevewache

    good value pharmacy Prime India Meds canada pharmacy reviews

  18. https://moxxtv.com/ Avatar

    References:

    Best steroid stack for beginners

    References:
    http://1.95.120.11:3000/isabellemocatt

  19. Michaelvop Avatar
    Michaelvop

    legit mexican pharmacy without prescription: mexican pharmacies no prescription – mexico pharmacies online

  20. movieru.jp Avatar

    References:

    Liquid testosterone for sale

    References:
    https://tea.neuron.my/charlottestabi

  21. 120.77.94.227 Avatar

    References:

    Strongest fat burner steroid

    References:
    https://rlgit.pro/valliexlt65013

  22. https://code.sharp8n.com/joesphmonroe59 Avatar

    References:

    Supplements like steroids but legal

    References:
    http://183.236.243.71:3693/elizabethwhite

  23. Edwardkeync Avatar
    Edwardkeync

    https://primeindiameds.shop/# escrow pharmacy online

  24. MatthewBom Avatar
    MatthewBom

    global pharmacy canada: NorthCareRx – NorthCareRx

  25. http://120.201.125.140/ Avatar

    References:

    Are all bodybuilders on steroids

    References:
    https://txuki.duckdns.org/katrinchism29

  26. 8.153.108.90 Avatar

    References:

    Lose steroid weight

    References:
    http://112.74.106.216:3000/lesleymorrison

  27. http://gitea.huangyanjie.com/laureneperkin5 Avatar

    References:

    Mental health cases in steroid abuse|acybgntbgv0jfnkoyks0e75iu3dejdmsdw:***

    References:
    http://115.159.107.117:3000/miguel57068348/159.75.27.1148705/wiki/Ventipulmin-Fachinformation

  28. https://askmilton.tv/ Avatar

    References:

    Crazy mass bulking stack review

    References:
    https://git.e-drones.com/taneshajeppese

  29. PhillipStibe Avatar
    PhillipStibe

    https://primeindiameds.com/# cialis canadian pharmacy

  30. ajmix.in Avatar

    References:

    Winstrol steroids

    References:
    https://esvoe.video/@wandaleschen76?page=about

  31. blackvision.co.uk Avatar

    References:

    Purchasing anabolic steroids online

    References:
    http://123.56.90.5:3000/newtonchen5382

  32. http://139.196.179.195:3000/kfgralph95969 Avatar

    References:

    Sports with alternatives to steroids

    References:
    https://mailppto.top/jeanamagarey79

  33. srsbkn.eu.org Avatar

    References:

    Anabolics for sale

    References:
    https://git.4lsa.com/bobv7166877761

  34. https://laviesound.com/toshabarnum79 Avatar

    References:

    Anabolic steroid side effects pictures

    References:
    https://datingmywish.com/@buckbyars12555

  35. git.lmbrs.org Avatar

    References:

    Liquid testosterone for sale

    References:
    https://tianqing999.com/kristandolan0

  36. https://git.ccmhub.se/ Avatar

    References:

    What’s the best testosterone steroid

    References:
    http://221.226.114.142:13000/freddywjq61721

  37. http://43.139.240.37/ Avatar

    References:

    How fast do steroids work

    References:
    http://47.105.50.196/joannwhitelegg

  38. 82.156.89.21 Avatar

    References:

    What happens when anabolic steroids are used in high doses?

    References:
    http://154.8.164.149:3000/lavinastrode28

  39. https://git.cloudsean.com/peteutz475864 Avatar

    References:

    How to get big fast without steroids

    References:
    https://dreamplacesai.de/georgiannafann

  40. https://vlotube.com Avatar

    References:

    What is it called when the testes are affected by steriods

    References:
    https://git.vezpi.com/rebekahcallagh

  41. Stevewache Avatar
    Stevewache

    mexican pharmacies no prescription BorderCare RX reputable pharmacies in mexico

  42. dev-gitlab.dev.sww.com.cn Avatar

    References:

    Pro clinical muscle stack

    References:
    http://47.103.78.70:3000/wyattmcevilly8

  43. https://git.4lsa.com/ Avatar

    References:

    Decca steroids for sale

    References:
    http://38.47.180.165:3000/mavissylvia855

Leave a Reply

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