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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

1
2
$myLongUrl = 'http://www.jeremyglover.com/blog/';
$myShortUrl = bitlyShorten($myLongUrl);
Share

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>