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(); } }