File indexing completed on 2025-01-26 05:29:54

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_ShortUrl
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Service_ShortUrl_AbstractShortener
0024  */
0025 // require_once 'Zend/Service/ShortUrl/AbstractShortener.php';
0026 
0027 /**
0028  * Bit.ly API implementation
0029  *
0030  * @category   Zend
0031  * @package    Zend_Service_ShortUrl
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Service_ShortUrl_BitLy extends Zend_Service_ShortUrl_AbstractShortener
0036 {
0037 
0038     /**
0039      * Base URI of the service
0040      *
0041      * @var string
0042      */
0043     protected $_apiUri = 'http://api.bitly.com';
0044 
0045     /**
0046      * user login name
0047      *
0048      * @var string
0049      */
0050     protected $_loginName;
0051 
0052     /**
0053      * user API key or application access token
0054      *
0055      * @var string
0056      */
0057     protected $_apiKey;
0058 
0059     /**
0060      * @param string $login user login name or application access token
0061      * @param null|string $apiKey user API key
0062      */
0063     public function __construct($login, $apiKey = null)
0064     {
0065         if(null === $apiKey) {
0066             $this->setOAuthAccessToken($login);
0067         } else {
0068             $this->setApiLogin($login, $apiKey);
0069         }
0070     }
0071 
0072     /**
0073      * set OAuth credentials
0074      *
0075      * @param $accessToken
0076      * @return Zend_Service_ShortUrl_BitLy
0077      */
0078     public function setOAuthAccessToken($accessToken)
0079     {
0080         $this->_apiKey = $accessToken;
0081         $this->_loginName = null;
0082         return $this;
0083     }
0084 
0085     /**
0086      * set login credentials
0087      *
0088      * @param $login
0089      * @param $apiKey
0090      * @return Zend_Service_ShortUrl_BitLy
0091      */
0092     public function setApiLogin($login, $apiKey)
0093     {
0094         $this->_apiKey = $apiKey;
0095         $this->_loginName = $login;
0096         return $this;
0097     }
0098 
0099     /**
0100      * prepare http client
0101      * @return void
0102      */
0103     protected function _setAccessParameter()
0104     {
0105         if(null === $this->_loginName) {
0106             //OAuth login
0107             $this->getHttpClient()->setParameterGet('access_token', $this->_apiKey);
0108         } else {
0109             //login/APIKey authentication
0110             $this->getHttpClient()->setParameterGet('login',$this->_loginName);
0111             $this->getHttpClient()->setParameterGet('apiKey',$this->_apiKey);
0112         }
0113     }
0114 
0115     /**
0116      * handle bit.ly response
0117      *
0118      * @return string
0119      * @throws Zend_Service_ShortUrl_Exception
0120      */
0121     protected function _processRequest()
0122     {
0123         $response = $this->getHttpClient()->request();
0124         if(500 == $response->getStatus()) {
0125             throw new Zend_Service_ShortUrl_Exception('Bit.ly :: '.$response->getBody());
0126         }
0127         return $response->getBody();
0128     }
0129 
0130     /**
0131      * This function shortens long url
0132      *
0133      * @param  string $url URL to Shorten
0134      * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
0135      * @return string Shortened Url
0136      */
0137     public function shorten($url)
0138     {
0139         $this->_validateUri($url);
0140         $this->_setAccessParameter();
0141 
0142         $this->getHttpClient()->setUri($this->_apiUri.'/v3/shorten');
0143         $this->getHttpClient()->setParameterGet('longUrl',$url);
0144         $this->getHttpClient()->setParameterGet('format','txt');
0145 
0146         return $this->_processRequest();
0147     }
0148 
0149     /**
0150      * Reveals target for short URL
0151      *
0152      * @param  string $shortenedUrl URL to reveal target of
0153      * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error
0154      * @return string Unshortened Url
0155      */
0156     public function unshorten($shortenedUrl)
0157     {
0158         $this->_validateUri($shortenedUrl);
0159         $this->_setAccessParameter();
0160 
0161         $this->getHttpClient()->setUri($this->_apiUri.'/v3/expand');
0162         $this->getHttpClient()->setParameterGet('shortUrl',$shortenedUrl);
0163         $this->getHttpClient()->setParameterGet('format','txt');
0164 
0165         return $this->_processRequest();
0166     }
0167 }