Zend Framework Action Helper For Bit.ly
Posted on : 06-09-2012 | By : TheBizzTech | In : Github, Model-View-Controller (MVC), PHP, Programming, Zend Framework
Tags: Bit.ly, Zend, Zend Framework
0
Wow, I just realized it has been a long time since I posted on my blog. If you care, it has been a few months since I left PayPal and I have been busy working at The Nerdery, mostly building Facebook Apps and the like. I am going to keep this post short and sweet.
The other day at work I learned I may need to integrate with the URL shortening service Bit.ly. My first reaction was why not use my own URL shortening service puni.co, and then I remembered I built that along time ago and never added an API and do not keep up development on it, so that idea was dead. Just as a little back story, as you may have guessed from the title, I mostly use Zend Framework now, because The Nerdery does and also because I used to only use Codeigniter but have come to love the power and flexibility of Zend.
Back to the main story. Once I learned I may need to use Bit.ly, I thought this would be the perfect time to build a class, or Action Helper on my own time since another project I am working on will be using Bit.ly, so was born my action helper found on Github. It was not until after I spent about an hour building this that I realized Zend already has nearly the exact same implementation already built in. After thinking about it I realized that is ok because they only built in the shortener part of the API, where I can now add more of Bit.ly services as I need then.
Enough talk, here is the code. Enjoy!!
CAUTION: If you are new to Zend and do not understand some of what I did then go to GitHub and read my README notes on implementation.
<?php
/**
* Action helper to use with Bit.ly url shortening service
*
* @category
* @package
* @subpackage
* @author Jason Michels
* @link http://thebizztech.com
* @version $Id$
*/
class Zendtest_Controller_Action_Helper_Bitly extends Zend_Controller_Action_Helper_Abstract
{
protected $apiUsername;
protected $apiKey;
protected $apiEndpoint;
/**
* Filter n array.
*
* @return void
*/
public function __construct($bootstrap = null)
{
$this->apiUsername = Zend_Registry::get('config')->bitly->apiUsername;
$this->apiKey = Zend_Registry::get('config')->bitly->apiKey;
$this->apiEndpoint = Zend_Registry::get('config')->bitly->apiEndpoint;
}
/**
* Shorten long url
*
* @param $longUrl string
* @param $domain string
* @return void
*/
public function shorten($longUrl, $domain = false)
{
$url = $this->apiEndpoint . '/v3/shorten?';
$url .= 'login='.$this->apiUsername.'&apiKey='.$this->apiKey;
$url .= '&longUrl='.$longUrl;
if($domain){
$url .= '&domain='.$domain;
}
//Use cUrl
$curlConfig = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => array(CURLOPT_FOLLOWLOCATION => true),
);
$client = new Zend_Http_Client($url, $curlConfig);
$response = $client->request("GET");
$response = json_decode($response->getBody());
return $response->data->url;
}
}

