|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
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. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Service_ShortUrl |
|
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 |
|
19 * @version $Id$ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Service_ShortUrl_AbstractShortener |
|
24 */ |
|
25 require_once 'Zend/Service/ShortUrl/AbstractShortener.php'; |
|
26 |
|
27 /** |
|
28 * Bit.ly API implementation |
|
29 * |
|
30 * @category Zend |
|
31 * @package Zend_Service_ShortUrl |
|
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 |
|
34 */ |
|
35 class Zend_Service_ShortUrl_BitLy extends Zend_Service_ShortUrl_AbstractShortener |
|
36 { |
|
37 |
|
38 /** |
|
39 * Base URI of the service |
|
40 * |
|
41 * @var string |
|
42 */ |
|
43 protected $_apiUri = 'http://api.bitly.com'; |
|
44 |
|
45 /** |
|
46 * user login name |
|
47 * |
|
48 * @var string |
|
49 */ |
|
50 protected $_loginName; |
|
51 |
|
52 /** |
|
53 * user API key or application access token |
|
54 * |
|
55 * @var string |
|
56 */ |
|
57 protected $_apiKey; |
|
58 |
|
59 /** |
|
60 * @param string $login user login name or application access token |
|
61 * @param null|string $apiKey user API key |
|
62 */ |
|
63 public function __construct($login, $apiKey = null) |
|
64 { |
|
65 if(null === $apiKey) { |
|
66 $this->setOAuthAccessToken($login); |
|
67 } else { |
|
68 $this->setApiLogin($login, $apiKey); |
|
69 } |
|
70 } |
|
71 |
|
72 /** |
|
73 * set OAuth credentials |
|
74 * |
|
75 * @param $accessToken |
|
76 * @return Zend_Service_ShortUrl_BitLy |
|
77 */ |
|
78 public function setOAuthAccessToken($accessToken) |
|
79 { |
|
80 $this->_apiKey = $accessToken; |
|
81 $this->_loginName = null; |
|
82 return $this; |
|
83 } |
|
84 |
|
85 /** |
|
86 * set login credentials |
|
87 * |
|
88 * @param $login |
|
89 * @param $apiKey |
|
90 * @return Zend_Service_ShortUrl_BitLy |
|
91 */ |
|
92 public function setApiLogin($login, $apiKey) |
|
93 { |
|
94 $this->_apiKey = $apiKey; |
|
95 $this->_loginName = $login; |
|
96 return $this; |
|
97 } |
|
98 |
|
99 /** |
|
100 * prepare http client |
|
101 * @return void |
|
102 */ |
|
103 protected function _setAccessParameter() |
|
104 { |
|
105 if(null === $this->_loginName) { |
|
106 //OAuth login |
|
107 $this->getHttpClient()->setParameterGet('access_token', $this->_apiKey); |
|
108 } else { |
|
109 //login/APIKey authentication |
|
110 $this->getHttpClient()->setParameterGet('login',$this->_loginName); |
|
111 $this->getHttpClient()->setParameterGet('apiKey',$this->_apiKey); |
|
112 } |
|
113 } |
|
114 |
|
115 /** |
|
116 * handle bit.ly response |
|
117 * |
|
118 * @return string |
|
119 * @throws Zend_Service_ShortUrl_Exception |
|
120 */ |
|
121 protected function _processRequest() |
|
122 { |
|
123 $response = $this->getHttpClient()->request(); |
|
124 if(500 == $response->getStatus()) { |
|
125 throw new Zend_Service_ShortUrl_Exception('Bit.ly :: '.$response->getBody()); |
|
126 } |
|
127 return $response->getBody(); |
|
128 } |
|
129 |
|
130 /** |
|
131 * This function shortens long url |
|
132 * |
|
133 * @param string $url URL to Shorten |
|
134 * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error |
|
135 * @return string Shortened Url |
|
136 */ |
|
137 public function shorten($url) |
|
138 { |
|
139 $this->_validateUri($url); |
|
140 $this->_setAccessParameter(); |
|
141 |
|
142 $this->getHttpClient()->setUri($this->_apiUri.'/v3/shorten'); |
|
143 $this->getHttpClient()->setParameterGet('longUrl',$url); |
|
144 $this->getHttpClient()->setParameterGet('format','txt'); |
|
145 |
|
146 return $this->_processRequest(); |
|
147 } |
|
148 |
|
149 /** |
|
150 * Reveals target for short URL |
|
151 * |
|
152 * @param string $shortenedUrl URL to reveal target of |
|
153 * @throws Zend_Service_ShortUrl_Exception if bit.ly reports an error |
|
154 * @return string Unshortened Url |
|
155 */ |
|
156 public function unshorten($shortenedUrl) |
|
157 { |
|
158 $this->_validateUri($shortenedUrl); |
|
159 $this->_setAccessParameter(); |
|
160 |
|
161 $this->getHttpClient()->setUri($this->_apiUri.'/v3/expand'); |
|
162 $this->getHttpClient()->setParameterGet('shortUrl',$shortenedUrl); |
|
163 $this->getHttpClient()->setParameterGet('format','txt'); |
|
164 |
|
165 return $this->_processRequest(); |
|
166 } |
|
167 } |