Archive for the ‘url’ tag
Shortening URLs using bit.ly API and PHP+Zend Framework
Recently for Topify I needed to shorten some long urls (our invite links) and my obvious choice of shortener was bit.ly. I tried looking for PHP wrapper for their API, but only found this project which was way more than I needed. So I’ve decided to write my own simple client using the Zend Framework HTTP Client. Here’s the result:
<?php
function bitly_shorten ($url) {
$client = new Zend_Http_Client('http://api.bit.ly/shorten');
$client->setParameterGet(array(
'version' => '2.0.1',
'longUrl' => $url,
'login' => 'arikfr',
'apiKey' => 'R_03feadf27c1c7c1a1ac3c3e7d6d41a27'
));
$response = $client->request();
if ($response->isSuccessful() ) {
$phpNative = Zend_Json::decode($response->getBody());
if ($phpNative['errorCode']==0) {
return $phpNative['results'][$url]['shortUrl'];
}
}
return "";
}
?>
As I said – it’s simple. There’s a lot more to do with it, but it serves the purpose I needed it for and I think it might be useful for others who only want to shorten some urls. If you need more than that, you might want to take a look at the above mentioned project.
Arik