Shorten URLs with Zend Framework and bit.ly

This function uses version 3.0 of the bit.ly API. You’ll need to register for an account with them if you don’t already have one. Then you can retrieve your API key and begin using it immediately.

function bitlyShorten($url)
{
	$client = new Zend_Http_Client('http://api.bit.ly/v3/shorten');

	$client->setParameterGet(array(
		'longUrl' => $url,
		'login' => 'xyz',
		'apiKey' => 'xyz'
	));

	$response = $client->request();

	if($response->isSuccessful())
	{
		$response = Zend_Json::decode($response->getBody());
		
		if($response['status_code'] == 200)
		{
			return $response['data']['url'];
		}
	}
	
	return (false);
}

Example:

$myLongUrl = 'https://www.jeremyglover.com/';
$myShortUrl = bitlyShorten($myLongUrl);
Share

Posted

in

, , , ,

by

Tags:

Comments

Leave a Reply

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