diff -r 877f952ae2bd -r 6b6c2214f778 web/lib/Zend/Service/ShortUrl/BitLy.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/lib/Zend/Service/ShortUrl/BitLy.php Thu Mar 21 19:52:38 2013 +0100 @@ -0,0 +1,167 @@ +setOAuthAccessToken($login); + } else { + $this->setApiLogin($login, $apiKey); + } + } + + /** + * set OAuth credentials + * + * @param $accessToken + * @return Zend_Service_ShortUrl_BitLy + */ + public function setOAuthAccessToken($accessToken) + { + $this->_apiKey = $accessToken; + $this->_loginName = null; + return $this; + } + + /** + * set login credentials + * + * @param $login + * @param $apiKey + * @return Zend_Service_ShortUrl_BitLy + */ + public function setApiLogin($login, $apiKey) + { + $this->_apiKey = $apiKey; + $this->_loginName = $login; + return $this; + } + + /** + * prepare http client + * @return void + */ + protected function _setAccessParameter() + { + if(null === $this->_loginName) { + //OAuth login + $this->getHttpClient()->setParameterGet('access_token', $this->_apiKey); + } else { + //login/APIKey authentication + $this->getHttpClient()->setParameterGet('login',$this->_loginName); + $this->getHttpClient()->setParameterGet('apiKey',$this->_apiKey); + } + } + + /** + * handle bit.ly response + * + * @return string + * @throws Zend_Service_ShortUrl_Exception + */ + protected function _processRequest() + { + $response = $this->getHttpClient()->request(); + if(500 == $response->getStatus()) { + throw new Zend_Service_ShortUrl_Exception('Bit.ly :: '.$response->getBody()); + } + return $response->getBody(); + } + + /** + * This function shortens long url + * + * @param string $url URL to Shorten + * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error + * @return string Shortened Url + */ + public function shorten($url) + { + $this->_validateUri($url); + $this->_setAccessParameter(); + + $this->getHttpClient()->setUri($this->_apiUri.'/v3/shorten'); + $this->getHttpClient()->setParameterGet('longUrl',$url); + $this->getHttpClient()->setParameterGet('format','txt'); + + return $this->_processRequest(); + } + + /** + * Reveals target for short URL + * + * @param string $shortenedUrl URL to reveal target of + * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error + * @return string Unshortened Url + */ + public function unshorten($shortenedUrl) + { + $this->_validateUri($shortenedUrl); + $this->_setAccessParameter(); + + $this->getHttpClient()->setUri($this->_apiUri.'/v3/expand'); + $this->getHttpClient()->setParameterGet('shortUrl',$shortenedUrl); + $this->getHttpClient()->setParameterGet('format','txt'); + + return $this->_processRequest(); + } +}