web/lib/Zend/Service/ShortUrl/IsGd.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Service_ShortUrl
    16  * @package    Zend_Service_ShortUrl
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: $
    19  * @version    $Id: $
    20  */
    20  */
    21 
    21 
    22 /**
    22 /**
    27 /**
    27 /**
    28  * Is.gd API implementation
    28  * Is.gd API implementation
    29  *
    29  *
    30  * @category   Zend
    30  * @category   Zend
    31  * @package    Zend_Service_ShortUrl
    31  * @package    Zend_Service_ShortUrl
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    34  */
    34  */
    35 class Zend_Service_ShortUrl_IsGd extends Zend_Service_ShortUrl_AbstractShortener
    35 class Zend_Service_ShortUrl_IsGd extends Zend_Service_ShortUrl_AbstractShortener
    36 {
    36 {
    37     /**
    37     /**
    38      * Base URI of the service
    38      * Base URI of the service
    39      *
    39      *
    40      * @var string
    40      * @var string
    41      */
    41      */
    42     protected $_baseUri = 'http://is.gd';
    42     protected $_baseUri = 'http://is.gd';
    43     
    43 
    44     /**
    44     /**
    45      * This function shortens long url
    45      * This function shortens long url
    46      *
    46      *
    47      * @param string $url URL to Shorten
    47      * @param string $url URL to Shorten
    48      * @throws Zend_Service_ShortUrl_Exception When URL is not valid
    48      * @throws Zend_Service_ShortUrl_Exception When URL is not valid
    49      * @return string New URL
    49      * @return string New URL
    50      */
    50      */
    51     public function shorten($url)
    51     public function shorten($url)
    52     {
    52     {
    53         $this->_validateUri($url);
    53         $this->_validateUri($url);
    54         
    54 
    55         $serviceUri = 'http://is.gd/api.php';
    55         $serviceUri = 'http://is.gd/api.php';
    56         
    56 
    57         $this->getHttpClient()->resetParameters(true);
    57         $this->getHttpClient()->resetParameters(true);
    58         $this->getHttpClient()->setUri($serviceUri);
    58         $this->getHttpClient()->setUri($serviceUri);
    59         $this->getHttpClient()->setParameterGet('longurl', $url);
    59         $this->getHttpClient()->setParameterGet('longurl', $url);
    60         
    60 
    61         $response = $this->getHttpClient()->request();
    61         $response = $this->getHttpClient()->request();
    62         
    62 
    63         return $response->getBody();
    63         return $response->getBody();
    64     }
    64     }
    65 
    65 
    66    /**
    66    /**
    67      * Reveals target for short URL
    67      * Reveals target for short URL
    73     public function unshorten($shortenedUrl)
    73     public function unshorten($shortenedUrl)
    74     {
    74     {
    75         $this->_validateUri($shortenedUrl);
    75         $this->_validateUri($shortenedUrl);
    76 
    76 
    77         $this->_verifyBaseUri($shortenedUrl);
    77         $this->_verifyBaseUri($shortenedUrl);
    78         
    78 
    79         $this->getHttpClient()->resetParameters(true);
    79         $this->getHttpClient()->resetParameters(true);
    80         $this->getHttpClient()->setUri($shortenedUrl);
    80         $this->getHttpClient()->setUri($shortenedUrl);
    81         $this->getHttpClient()->setConfig(array('maxredirects' => 0));
    81         $this->getHttpClient()->setConfig(array('maxredirects' => 0));
    82         
    82 
    83         $response = $this->getHttpClient()->request();
    83         $response = $this->getHttpClient()->request();
    84         if ($response->isError()) {
    84         if ($response->isError()) {
    85             require_once 'Zend/Service/ShortUrl/Exception.php';
    85             require_once 'Zend/Service/ShortUrl/Exception.php';
    86             throw new Zend_Service_ShortUrl_Exception($response->getMessage());
    86             throw new Zend_Service_ShortUrl_Exception($response->getMessage());
    87         }
    87         }
    88         
    88 
    89         if ($response->isRedirect()) {
    89         if ($response->isRedirect()) {
    90             return $response->getHeader('Location');
    90             return $response->getHeader('Location');
    91         }
    91         }
    92         
    92 
    93         require_once 'Zend/Service/ShortUrl/Exception.php';
    93         require_once 'Zend/Service/ShortUrl/Exception.php';
    94         throw new Zend_Service_ShortUrl_Exception('Url unshortening was not successful');
    94         throw new Zend_Service_ShortUrl_Exception('Url unshortening was not successful');
    95     }
    95     }
    96 }
    96 }