File indexing completed on 2024-06-16 05:30:25

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  * Is.gd 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_IsGd extends Zend_Service_ShortUrl_AbstractShortener
0036 {
0037     /**
0038      * Base URI of the service
0039      *
0040      * @var string
0041      */
0042     protected $_baseUri = 'http://is.gd';
0043 
0044     /**
0045      * This function shortens long url
0046      *
0047      * @param string $url URL to Shorten
0048      * @throws Zend_Service_ShortUrl_Exception When URL is not valid
0049      * @return string New URL
0050      */
0051     public function shorten($url)
0052     {
0053         $this->_validateUri($url);
0054 
0055         $serviceUri = 'http://is.gd/api.php';
0056 
0057         $this->getHttpClient()->resetParameters(true);
0058         $this->getHttpClient()->setUri($serviceUri);
0059         $this->getHttpClient()->setParameterGet('longurl', $url);
0060 
0061         $response = $this->getHttpClient()->request();
0062 
0063         return $response->getBody();
0064     }
0065 
0066    /**
0067      * Reveals target for short URL
0068      *
0069      * @param string $shortenedUrl URL to reveal target of
0070      * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
0071      * @return string
0072      */
0073     public function unshorten($shortenedUrl)
0074     {
0075         $this->_validateUri($shortenedUrl);
0076 
0077         $this->_verifyBaseUri($shortenedUrl);
0078 
0079         $this->getHttpClient()->resetParameters(true);
0080         $this->getHttpClient()->setUri($shortenedUrl);
0081         $this->getHttpClient()->setConfig(array('maxredirects' => 0));
0082 
0083         $response = $this->getHttpClient()->request();
0084         if ($response->isError()) {
0085             // require_once 'Zend/Service/ShortUrl/Exception.php';
0086             throw new Zend_Service_ShortUrl_Exception($response->getMessage());
0087         }
0088 
0089         if ($response->isRedirect()) {
0090             return $response->getHeader('Location');
0091         }
0092 
0093         // require_once 'Zend/Service/ShortUrl/Exception.php';
0094         throw new Zend_Service_ShortUrl_Exception('Url unshortening was not successful');
0095     }
0096 }