This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bitly.php
executable file
·794 lines (640 loc) · 20.8 KB
/
bitly.php
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
<?php
/**
* Bitly class
*
* This source file can be used to communicate with Bit.ly (http://bit.ly)
*
* The class is documented in the file itself. If you find any bugs help me out and report them. Reporting can be done by sending an email to php-bitly-bugs[at]verkoyen[dot]eu.
* If you report a bug, make sure you give me enough information (include your code).
*
* Changelog since 2.0.1
* - added some new methods to reflect the lateste version of the API, new methods are:
* - clicksByDay
*
* @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
* @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
* @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
* @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
* @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
*
*
* Changelog since 2.0.0
* - added some new method to reflect the latest version of the API, new methods are:
* - clicksByMinute
* - countries
* - info
* - lookup
* - referrers
*
* Changelog since 1.0.2
* - now using version 3 of the bit.ly API.
*
* Changelog since 1.0.1
* - each URL will be added into your history, removed the history parameter
*
* Changelog since 1.0.0
* - corrected some documentation
* - wrote some explanation for the method-parameters
*
* License
* Copyright (c) 2010, Tijs Verkoyen. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
*
* This software is provided by the author "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the author be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
*
* @author Tijs Verkoyen <[email protected]>
* @version 2.0.2
*
* @copyright Copyright (c) 2010, Tijs Verkoyen. All rights reserved.
* @license BSD License
*/
class Bitly
{
// internal constant to enable/disable debugging
const DEBUG = false;
// url for the bitly-api
const API_URL = 'http://api.bit.ly/v3';
// port for the bitly-API
const API_PORT = 80;
// bitly-API version
const API_VERSION = '3.0';
// current version
const VERSION = '2.0.2';
/**
* The API-key that will be used for authenticating
*
* @var string
*/
private $apiKey;
/**
* The login that will be used for authenticating
*
* @var string
*/
private $login;
/**
* The timeout
*
* @var int
*/
private $timeOut = 60;
/**
* The user agent
*
* @var string
*/
private $userAgent;
// class methods
/**
* Default constructor
*
* @return void
* @param string $login The login (username) that has to be used for authenticating.
* @param string $apiKey The API-key that has to be used for authentication (see http://bit.ly/account).
*/
public function __construct($login, $apiKey)
{
$this->setLogin($login);
$this->setApiKey($apiKey);
}
/**
* Make the call
*
* @return string
* @param string $url The url to call.
* @param array[optional] $aParameters The parameters to pass.
*/
private function doCall($url, $aParameters = array())
{
// redefine
$url = (string) $url;
$aParameters = (array) $aParameters;
// add required parameters
$aParameters['format'] = 'json';
$aParameters['login'] = $this->getLogin();
$aParameters['apiKey'] = $this->getApiKey();
// init var
$queryString = '';
// loop parameters and add them to the queryString
foreach($aParameters as $key => $value) $queryString .= '&' . $key . '=' . urlencode(utf8_encode($value));
// cleanup querystring
$queryString = trim($queryString, '&');
// append to url
$url .= '?' . $queryString;
// prepend
$url = self::API_URL . '/' . $url;
// set options
$options[CURLOPT_URL] = $url;
$options[CURLOPT_PORT] = self::API_PORT;
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
if(ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) $options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
// init
$curl = curl_init();
// set options
curl_setopt_array($curl, $options);
// execute
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
// fetch errors
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
// close
curl_close($curl);
// invalid headers
if(!in_array($headers['http_code'], array(0, 200)))
{
// should we provide debug information
if(self::DEBUG)
{
// make it output proper
echo '<pre>';
// dump the header-information
var_dump($headers);
// dump the raw response
var_dump($response);
// end proper format
echo '</pre>';
// stop the script
exit;
}
// throw error
throw new BitlyException('Invalid headers (' . $headers['http_code'] . ')', (int) $headers['http_code']);
}
// error?
if($errorNumber != '') throw new BitlyException($errorMessage, $errorNumber);
// we expect JSON so decode it
$json = @json_decode($response, true);
// validate json
if($json === false) throw new BitlyException('Invalid JSON-response');
// is error?
if(!isset($json['status_txt']) || (string) $json['status_txt'] != 'OK')
{
// bitly-error?
if(isset($json['status_code']) && isset($json['status_txt'])) throw new BitlyException((string) $json['status_txt'], (int) $json['status_code']);
// invalid json?
else throw new BitlyException('Invalid JSON-response');
}
// return
return $json;
}
/**
* Get the APIkey
*
* @return string
*/
private function getApiKey()
{
return (string) $this->apiKey;
}
/**
* Get the login
*
* @return string
*/
private function getLogin()
{
return (string) $this->login;
}
/**
* Get the timeout that will be used
*
* @return int
*/
public function getTimeOut()
{
return (int) $this->timeOut;
}
/**
* Get the useragent that will be used. Our version will be prepended to yours.
* It will look like: "PHP Bitly/<version> <your-user-agent>"
*
* @return string
*/
public function getUserAgent()
{
return (string) 'PHP Bitly/' . self::VERSION . ' ' . $this->userAgent;
}
/**
* Set the API-key that has to be used
*
* @return void
* @param string $apiKey The key to set.
*/
private function setApiKey($apiKey)
{
$this->apiKey = (string) $apiKey;
}
/**
* Set the login that has to be used
*
* @return void
* @param string $login The login to use.
*/
private function setLogin($login)
{
$this->login = (string) $login;
}
/**
* Set the timeout
* After this time the request will stop. You should handle any errors triggered by this.
*
* @return void
* @param int $seconds The timeout in seconds.
*/
public function setTimeOut($seconds)
{
$this->timeOut = (int) $seconds;
}
/**
* Set the user-agent for you application
* It will be appended to ours, the result will look like: "PHP Bitly/<version> <your-user-agent>"
*
* @return void
* @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>.
*/
public function setUserAgent($userAgent)
{
$this->userAgent = (string) $userAgent;
}
// url methods
/**
* Given a bit.ly URL or hash, expand decodes it and returns back the target URL.
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function expand($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('expand', $parameters);
// validate
if(isset($response['data']['expand'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['expand'][0]['short_url'];
$return['long_url'] = $response['data']['expand'][0]['long_url'];
$return['hash'] = $response['data']['expand'][0]['user_hash'];
$return['global_hash'] = $response['data']['expand'][0]['global_hash'];
// return
return $return;
}
// fallback
return false;
}
/**
* This is used to return the page title for a given bit.ly link.
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function info($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('info', $parameters);
// validate
if(isset($response['data']['info'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['info'][0]['short_url'];
$return['hash'] = $response['data']['info'][0]['user_hash'];
$return['global_hash'] = $response['data']['info'][0]['global_hash'];
$return['created_by'] = $response['data']['info'][0]['created_by'];
$return['title'] = $response['data']['info'][0]['title'];
// return
return $return;
}
// fallback
return false;
}
/**
* This is used to query for a bit.ly link based on a long URL. For example you would use /v3/lookup followed by /v3/clicks to find click data when you have a long URL to start with.
*
* @return array
* @param string $url A long URL to shorten, eg: http://betaworks.com.
*/
public function lookup($url)
{
// redefine
$url = (string) $url;
// make the call
$parameters['url'] = $url;
// make the call
$response = $this->doCall('lookup', $parameters);
// validate
if(isset($response['data']['lookup'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['lookup'][0]['short_url'];
$return['long_url'] = $response['data']['lookup'][0]['url'];
$return['global_hash'] = $response['data']['lookup'][0]['global_hash'];
// return
return $return;
}
// fallback
return false;
}
/**
* For a long URL, shorten encodes a URL and returns a short one.
*
* @return array
* @param string $url A long URL to shorten, eg: http://betaworks.com.
* @param string[optional] $domain Refers to a preferred domain, possible values are: bit.ly or j.mp.
* @param string[optional] $endUserLogin The end users login.
* @param string[optional] $endUserApiKey The end users apiKey.
*/
public function shorten($url, $domain = null, $endUserLogin = null, $endUserApiKey = null)
{
// domain specified
if($domain !== null && !in_array((string) $domain, array('bit.ly', 'j.mp'))) throw new BitlyException('Invalid domain, only bit.ly or j.mp are allowed, custom domains will be handled by using the correct login.');
// redefine
$parameters['longUrl'] = (string) $url;
if($domain !== null) $parameters['domain'] = (string) $domain;
if($endUserLogin !== null) $parameters['x_login'] = (string) $endUserLogin;
if($endUserApiKey !== null) $parameters['x_apiKey'] = (string) $endUserApiKey;
// make the call
$response = $this->doCall('shorten', $parameters);
// validate
if(isset($response['data']))
{
// init var
$result = array();
$result['url'] = $response['data']['url'];
$result['long_url'] = $response['data']['long_url'];
$result['hash'] = $response['data']['hash'];
$result['global_hash'] = $response['data']['global_hash'];
$result['is_new_hash'] = ($response['data']['new_hash'] == 1);
return $result;
}
// fallback
return false;
}
// statistic methods
/**
* Grab the statistics about a link
*
* @return array
* @param string[optional] $shortURL refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash refers to a bit.ly hash eg: 1RmnUT.
*/
public function clicks($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('clicks', $parameters);
// validate
if(isset($response['data']['clicks'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['clicks'][0]['short_url'];
$return['hash'] = $response['data']['clicks'][0]['user_hash'];
$return['clicks'] = (int) $response['data']['clicks'][0]['user_clicks'];
$return['global_hash'] = $response['data']['clicks'][0]['global_hash'];
$return['global_clicks'] = (int) $response['data']['clicks'][0]['global_clicks'];
// return
return $return;
}
// fallback
return false;
}
/**
* Given a bit.ly URL or hash, provides time series clicks per day for the last 30 days in reverse chronological order (most recent to least recent).
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function clicksByDay($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('clicks_by_day', $parameters);
// validate
if(isset($response['data']['clicks_by_day'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['clicks_by_day'][0]['short_url'];
$return['hash'] = $response['data']['clicks_by_day'][0]['user_hash'];
$return['global_hash'] = $response['data']['clicks_by_day'][0]['global_hash'];
$return['clicks'] = (array) $response['data']['clicks_by_day'][0]['clicks'];
// return
return $return;
}
// fallback
return false;
}
/**
* Given a bit.ly URL or hash, provides time series clicks per minute for the last hour in reverse chronological order (most recent to least recent).
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function clicksByMinute($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('clicks_by_minute', $parameters);
// validate
if(isset($response['data']['clicks_by_minute'][0]))
{
// init var
$return = array();
$return['url'] = $response['data']['clicks_by_minute'][0]['short_url'];
$return['hash'] = $response['data']['clicks_by_minute'][0]['user_hash'];
$return['global_hash'] = $response['data']['clicks_by_minute'][0]['global_hash'];
$return['clicks'] = (array) $response['data']['clicks_by_minute'][0]['clicks'];
// return
return $return;
}
// fallback
return false;
}
/**
* Provides a list of countries from which clicks on a specified bit.ly short link have originated, and the number of clicks per country.
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function countries($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('countries', $parameters);
// validate
if(isset($response['data']['countries']))
{
// init var
$return = array();
$return['url'] = $response['data']['short_url'];
$return['hash'] = $response['data']['user_hash'];
$return['global_hash'] = $response['data']['global_hash'];
$return['countries'] = (array) $response['data']['countries'];
// return
return $return;
}
// fallback
return false;
}
/**
* Provides a list of referring sites for a specified bit.ly short link, and the number of clicks per referrer.
*
* @return array
* @param string[optional] $shortURL Refers to a bit.ly URL eg: http://bit.ly/1RmnUT.
* @param string[optional] $hash Refers to a bit.ly hash eg: 1RmnUT.
*/
public function referrers($shortURL = null, $hash = null)
{
// redefine
$shortURL = (string) $shortURL;
$hash = (string) $hash;
// validate
if($shortURL == '' && $hash == '') throw new BitlyException('shortURL or hash should contain a value');
// make the call
if($shortURL !== '') $parameters['shortUrl'] = $shortURL;
if($hash !== '') $parameters['hash'] = $hash;
// make the call
$response = $this->doCall('referrers', $parameters);
// validate
if(isset($response['data']['referrers']))
{
// init var
$return = array();
$return['url'] = $response['data']['short_url'];
$return['hash'] = $response['data']['user_hash'];
$return['global_hash'] = $response['data']['global_hash'];
$return['created_by'] = $response['data']['created_by'];
$return['referrers'] = $response['data']['referrers'];
// return
return $return;
}
// fallback
return false;
}
// user methods
public function authenticate($endUserLogin, $endUserPassword)
{
// redefine
$parameters['x_login'] = (string) $endUserLogin;
$parameters['x_password'] = (string) $endUserPassword;
// make the call
$response = $this->doCall('authenticate', $parameters);
// @todo implement http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/authenticate
// validate
if(isset($response['data']['valid']))
{
return ($response['data']['valid'] == 1);
}
// fallback
return false;
}
/**
* This is used to query whether a given short domain is assigned for bitly.Pro, and is consequently a valid shortUrl parameter for other api calls.
*
* @return bool
* @param string $domain A short domain.
*/
public function isProDomain($domain)
{
// redefine
$parameters['domain'] = (string) $domain;
// make the call
$response = $this->doCall('bitly_pro_domain', $parameters);
// validate
if(isset($response['data']['bitly_pro_domain']))
{
return ($response['data']['bitly_pro_domain'] == 1);
}
// fallback
return false;
}
/**
* For any given a bit.ly user login and apiKey, you can validate that the pair is active.
*
* @return bool
* @param string $endUserLogin The end users login.
* @param string $endUserApiKey The end users apiKey.
*/
public function validate($endUserLogin, $endUserApiKey)
{
// redefine
$parameters['x_login'] = (string) $endUserLogin;
$parameters['x_apiKey'] = (string) $endUserApiKey;
// make the call
$response = $this->doCall('validate', $parameters);
// validate
if(isset($response['data']['valid']))
{
return ($response['data']['valid'] == 1);
}
// fallback
return false;
}
}
/**
* Bitly Exception class
*
* @author Tijs Verkoyen <[email protected]>
*/
class BitlyException extends Exception
{
}
?>