File indexing completed on 2024-12-22 05:36:17

0001 <?php
0002 
0003 /**
0004  * @see Zend_Service_ShortUrl_AbstractShortener
0005  */
0006 require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
0007 
0008 /**
0009  * bitly.com API implementation
0010  *
0011  * @category   Zend
0012  * @package    Zend_Service_ShortUrl
0013  * @copyright  Copyright (c) 2011 Björn Schramke (http://www.schramke-online.de)
0014  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0015  */
0016 class CB_Service_ShortUrl_BitlyCom extends Zend_Service_ShortUrl_AbstractShortener
0017 {
0018     /**
0019      * Base URI of the service
0020      *
0021      * @var string
0022      */
0023     protected $_baseUri = 'http://bit.ly';
0024     protected $_apiBaseUri = 'http://api.bitly.com';
0025 
0026     /**
0027      * The bitly.com login
0028      *
0029      * @var string
0030      */
0031     protected $_login = null;
0032 
0033     /**
0034      * The bitly.com API-Key
0035      *
0036      * @var string
0037      */
0038     protected $_apiKey = null;
0039 
0040     /**
0041      * The default response-format for bitly.com-calls
0042      *
0043      * @var string ( txt | xml | json )
0044      */
0045     protected $_format = 'txt';
0046 
0047     public function __construct($login,$apiKey,$format='txt') 
0048     {
0049         $this->setLogin($login);
0050         $this->setApiKey($apiKey);
0051         $this->setFormat($format);
0052     }
0053     
0054     /**
0055      * This function shortens long url
0056      *
0057      * @param string $url URL to Shorten
0058      * @throws Zend_Service_ShortUrl_Exception When URL is not valid
0059      * @return string New URL
0060      */
0061     public function shorten($url)
0062     {
0063         $this->_validateUri($url);
0064 
0065         $serviceUri = $this->_apiBaseUri.'/v3/shorten';
0066         $httpClient = $this->getHttpClient();
0067 
0068         $httpClient->setUri($serviceUri);
0069         $httpClient->setMethod('GET');
0070         $httpClient->setParameterGet('login', $this->_login);
0071         $httpClient->setParameterGet('apiKey', $this->_apiKey);
0072         $httpClient->setParameterGet('longUrl', $url);
0073         $httpClient->setParameterGet('format', $this->_format);
0074         
0075         $response = $httpClient->request();
0076 
0077         return $response->getBody();
0078     }
0079 
0080     /**
0081      * Reveals target for short URL
0082      *
0083      * @param string $shortenedUrl URL to reveal target of
0084      * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
0085      * @return string
0086      */
0087     public function unshorten($shortenedUrl)
0088     {
0089         $this->_validateUri($shortenedUrl);
0090         $this->_verifyBaseUri($shortenedUrl);
0091         
0092         $serviceUri = $this->_apiBaseUri.'/v3/expand';
0093         $httpClient = $this->getHttpClient();
0094 
0095         $httpClient->setUri($serviceUri);
0096         $httpClient->setMethod('GET');
0097         $httpClient->setParameterGet('login', $this->_login);
0098         $httpClient->setParameterGet('apiKey', $this->_apiKey);
0099         $httpClient->setParameterGet('shortUrl', $shortenedUrl);
0100         $httpClient->setParameterGet('format', $this->_format);
0101         
0102         $response = $httpClient->request();
0103 
0104         return $response->getBody();
0105     }
0106     
0107     public function setLogin($login)
0108     {
0109         $this->_login = (string)$login;
0110         
0111         return $this;
0112     }
0113 
0114     public function getLogin()
0115     {
0116         return $this->_login;
0117     }
0118     
0119     public function setApiKey($apiKey)
0120     {
0121         $this->_apiKey= (string)$apiKey;
0122         
0123         return $this;
0124     }
0125 
0126     public function getApiKey()
0127     {
0128         return $this->_apiKey;
0129     }
0130     
0131     public function setFormat($format)
0132     {
0133         if( !is_string($format) )
0134             $this->_format = 'txt';
0135             return $this;
0136             
0137         switch($format)
0138         {
0139             case 'xml':
0140             case 'json':
0141             case 'txt':
0142                 $this->_format = $format;
0143                 break;
0144             default:
0145                 $this->_format = 'txt';
0146                 break;
0147         }
0148         
0149         return $this;
0150     }
0151 
0152     public function getFormat()
0153     {
0154         return $this->_format;
0155     }
0156     
0157 }