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  * TinyUrl.com 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_TinyUrlCom extends Zend_Service_ShortUrl_AbstractShortener
0036 {
0037     /**
0038      * Base URI of the service
0039      *
0040      * @var string
0041      */
0042     protected $_baseUri = 'http://tinyurl.com';
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://tinyurl.com/api-create.php';
0056 
0057         $this->getHttpClient()->setUri($serviceUri);
0058         $this->getHttpClient()->setParameterGet('url', $url);
0059 
0060         $response = $this->getHttpClient()->request();
0061 
0062         return $response->getBody();
0063     }
0064 
0065     /**
0066      * Reveals target for short URL
0067      *
0068      * @param string $shortenedUrl URL to reveal target of
0069      * @throws Zend_Service_ShortUrl_Exception When URL is not valid or is not shortened by this service
0070      * @return string
0071      */
0072     public function unshorten($shortenedUrl)
0073     {
0074         $this->_validateUri($shortenedUrl);
0075 
0076         $this->_verifyBaseUri($shortenedUrl);
0077 
0078         //TinyUrl.com does not have an API for that, but we can use preview feature
0079         //we need new Zend_Http_Client
0080         $this->setHttpClient(new Zend_Http_Client());
0081 
0082         $this->getHttpClient()
0083              ->setCookie('preview', 1)
0084              ->setUri($shortenedUrl);
0085 
0086         //get response
0087         $response = $this->getHttpClient()->request();
0088 
0089         // require_once 'Zend/Dom/Query.php';
0090         $dom = new Zend_Dom_Query($response->getBody());
0091 
0092         //find the redirect url link
0093         $results = $dom->query('a#redirecturl');
0094 
0095         //get href
0096         $originalUrl = $results->current()->getAttribute('href');
0097 
0098         return $originalUrl;
0099     }
0100 }