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 * TinyUrl.com API implementation |
28 * TinyUrl.com 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_TinyUrlCom extends Zend_Service_ShortUrl_AbstractShortener |
35 class Zend_Service_ShortUrl_TinyUrlCom 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://tinyurl.com'; |
42 protected $_baseUri = 'http://tinyurl.com'; |
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 |
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://tinyurl.com/api-create.php'; |
55 $serviceUri = 'http://tinyurl.com/api-create.php'; |
56 |
56 |
57 $this->getHttpClient()->setUri($serviceUri); |
57 $this->getHttpClient()->setUri($serviceUri); |
58 $this->getHttpClient()->setParameterGet('url', $url); |
58 $this->getHttpClient()->setParameterGet('url', $url); |
59 |
59 |
60 $response = $this->getHttpClient()->request(); |
60 $response = $this->getHttpClient()->request(); |
61 |
61 |
62 return $response->getBody(); |
62 return $response->getBody(); |
63 } |
63 } |
64 |
64 |
65 /** |
65 /** |
66 * Reveals target for short URL |
66 * Reveals target for short URL |
70 * @return string |
70 * @return string |
71 */ |
71 */ |
72 public function unshorten($shortenedUrl) |
72 public function unshorten($shortenedUrl) |
73 { |
73 { |
74 $this->_validateUri($shortenedUrl); |
74 $this->_validateUri($shortenedUrl); |
75 |
75 |
76 $this->_verifyBaseUri($shortenedUrl); |
76 $this->_verifyBaseUri($shortenedUrl); |
77 |
77 |
78 //TinyUrl.com does not have an API for that, but we can use preview feature |
78 //TinyUrl.com does not have an API for that, but we can use preview feature |
79 //we need new Zend_Http_Client |
79 //we need new Zend_Http_Client |
80 $this->setHttpClient(new Zend_Http_Client()); |
80 $this->setHttpClient(new Zend_Http_Client()); |
81 |
81 |
82 $this->getHttpClient() |
82 $this->getHttpClient() |
83 ->setCookie('preview', 1) |
83 ->setCookie('preview', 1) |
84 ->setUri($shortenedUrl); |
84 ->setUri($shortenedUrl); |
85 |
85 |
86 //get response |
86 //get response |
87 $response = $this->getHttpClient()->request(); |
87 $response = $this->getHttpClient()->request(); |
88 |
88 |
89 require_once 'Zend/Dom/Query.php'; |
89 require_once 'Zend/Dom/Query.php'; |
90 $dom = new Zend_Dom_Query($response->getBody()); |
90 $dom = new Zend_Dom_Query($response->getBody()); |
91 |
91 |
92 //find the redirect url link |
92 //find the redirect url link |
93 $results = $dom->query('a#redirecturl'); |
93 $results = $dom->query('a#redirecturl'); |
94 |
94 |
95 //get href |
95 //get href |
96 $originalUrl = $results->current()->getAttribute('href'); |
96 $originalUrl = $results->current()->getAttribute('href'); |
97 |
97 |
98 return $originalUrl; |
98 return $originalUrl; |
99 } |
99 } |
100 } |
100 } |