-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.bitly.php
More file actions
executable file
·258 lines (239 loc) · 6.95 KB
/
class.bitly.php
File metadata and controls
executable file
·258 lines (239 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
class bitly{
private $login;
private $apikey;
private $version = '2.0.1';
private $format = 'json';
/**
* The raw data returned from bit.ly.
*
* @var array
*/
private $results;
/**
* The any error messages returned from bit.ly.
*
* @var mixed
*/
private $errors = false;
/**
* Constructor
*
* @param string $login The login to use for the connection.
* @param string $apikey The API key to use for the connection.
*/
public function bitly($login, $apikey)
{
$this->login = $login;
$this->apikey = $apikey;
}
/**
* Set the format of the data being returned, this can be in json or xml.
*
* @param string $format The format.
*/
public function setFormat($format)
{
$format = strtolower($format);
if ( $format == 'json' || $format == 'xml' ) {
$this->format = strtolower($format);
} else {
return 'Invalid format specified!';
}
}
/**
* Get the format of the returned data.
*
* @return string The format of the returned data.
*/
public function getFormat()
{
return $this->format;
}
/**
* Get the latest errors.
*
* @return array An array containing the latest errors.
*/
public function getErrors()
{
return $this->errors;
}
/**
* Get the latest results from the bit.ly service.
*
* @return array An array containing the bit.ly results.
*/
public function getRawResults()
{
return $this->results;
}
/**
* Shorten a URL using the bit.ly service.
*
* @param string $url The URL to shorten.
* @param boolean $returnHash Boolean value to make the function return an array
* containing the shortened URL and a hash.
*
* @return mixed Either the shortened URL or an array containing the shortened URL
* and the hash value returned from the site.
*/
public function shorten($url, $returnHash = false)
{
$bitlyurl = 'http://api.bit.ly/shorten?version='.$this->version.'&longUrl='.$url.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
if ( $this->getResult($bitlyurl) !== false ) {
if ( $returnHash == true ) {
return array('shortUrl' => $this->results['shortUrl'], 'hash' => $this->results['hash']);
} else {
return $this->results['shortUrl'];
}
}
return false;
}
/**
* Expand a URL that has been shortened using the bit.ly service.
*
* @param string $url The URL to expand.
* @param string $hash The hash value to be translated into a long URL.
*
* @return string The long URL, as translated by the bit.ly service.
*/
public function expand($shortUrl, $hash = '')
{
if ( $shortUrl != '' ) {
$bitlyurl = 'http://api.bit.ly/expand?version='.$this->version.'&shortUrl='.$shortUrl.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $shortUrl == '' && $hash != '' ) {
$bitlyurl = 'http://api.bit.ly/expand?version='.$this->version.'&hash='.$hash.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $this->getResult($bitlyurl) !== false ) {
return $this->results['longUrl'];
}
return false;
}
/**
* Find out information about the URL.
*
* @param string $url The shortened URL.
* @param string $hash The hash value from the bit.ly service.
* @param string $keys This will cause the bit.ly service to return only this data item.
*
* @return array The information about a URL from the bit.ly service.
*/
public function info($shortUrl, $hash = '', $keys = '')
{
if ( $shortUrl != '' ) {
$bitlyurl = 'http://api.bit.ly/info?version='.$this->version.'&shortUrl='.$shortUrl.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $shortUrl == '' && $hash != '' ) {
$bitlyurl = 'http://api.bit.ly/info?version='.$this->version.'&hash='.$hash.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $keys != '' ) {
$bitlyurl .= '&keys='.$keys;
}
if ( $this->getResult($shortUrl) !== false ) {
return $this->results;
}
return false;
}
/**
* Find out statistics about the URL.
*
* @param string $url The shortened URL.
* @param string $hash The hash value from the bit.ly service.
*
* @return array An array containing statistics about the URL in question.
*/
public function stats($shortUrl, $hash = '')
{
if ( $shortUrl != '' ) {
$bitlyurl = 'http://api.bit.ly/stats?version='.$this->version.'&shortUrl='.$shortUrl.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $shortUrl == '' && $hash != '' ) {
$bitlyurl = 'http://api.bit.ly/stats?version='.$this->version.'&hash='.$hash.'&login='.$this->login.'&apiKey='.$this->apikey.'&format='.$this->format;
}
if ( $this->getResult($bitlyurl) !== false ) {
return $this->results;
}
return false;
}
/**
* Return a list of all error codes from the bit.ly service.
*
* @return array An array containing all possible error codes.
*/
public function errors()
{
$bitlyurl = 'http://api.bit.ly/errors?version='.$this->version.'&login='.$this->login.'&apiKey='.$this->apikey;
if ( $this->getResult($bitlyurl, true) !== false ) {
return $this->results;
}
return false;
}
/**
* Get the results from a bit.ly service interaction.
*
* @param string $url The URL to interact with, the action of the interaction
* will be contained within the URL.
* @param boolean $errors Passed by the errors() function and forces this function
* to use json as the format.
*
* @return boolean True if everything has worked, otherwise false.
*/
private function getResult($bitlyurl, $errors = false )
{
if ( $errors ) {
$tmpFormat = $this->format;
$this->format = 'json';
}
if ( $this->format == 'json' ) {
$results = json_decode(file_get_contents($bitlyurl), true);
} else if ( $this->format == 'xml' ) {
$xml = file_get_contents($bitlyurl);
$results = $this->XML2Array($xml);
}
if ( $errors ) {
$this->format = $tmpFormat;
}
if ( $results['statusCode'] != 'OK' ) {
$this->errors = $results;
return false;
}
if ( $errors ) {
// Save everything in the results array
$this->results = $results['results'];
} else {
// Save the first item in the results array
$this->results = current($results['results']);
}
return true;
}
/**
* Convert an XML string into an array.
*
* @param string $xml The XML to convert.
* @param boolean $recursive Has the function called itself?
*
* @return array The short URL
*/
private function XML2Array($xml, $recursive = false)
{
if ( !$recursive ) {
$array = simplexml_load_string($xml);
} else {
$array = $xml;
}
$newArray = array();
$array = (array)$array;
foreach ( $array as $key => $value ) {
$value = (array)$value;
if ( isset($value[0]) ) {
$newArray[$key] = trim($value[0]);
} else {
$newArray[$key] = $this->XML2Array($value, true);
}
}
return $newArray;
}
}
?>