forked from Falicon/BitlyPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitly.php
817 lines (789 loc) · 27.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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
<?php
/**
* @file
* Simple PHP library for interacting with the v3 bit.ly api (only deals with
* JSON format, but supports new OAuth endpoints).
* REQUIREMENTS: PHP, Curl, JSON
*
* @link https://github.com/Falicon/BitlyPHP
* @author Kevin Marshall <info@falicon.com>
* @author Robin Monks <devlinks@gmail.com>
*/
/**
* The bitlyKey assigned to your bit.ly account. (http://bit.ly/a/account)
*/
define('bitlyKey', 'YOUR_BITLY_ASSIGNED_KEY');
/**
* The bitlyLogin assigned to your bit.ly account. (http://bit.ly/a/account)
*/
define('bitlyLogin' , 'YOUR_BITLY_LOGIN');
/**
* The client_id assigned to your OAuth app. (http://bit.ly/a/account)
*/
define('bitly_clientid' , 'YOUR_BITLY_ASSIGNED_CLIENT_ID_FOR_OAUTH');
/**
* The client_secret assigned to your OAuth app. (http://bit.ly/a/account)
*/
define('bitly_secret' , 'YOUR_BITLY_ASSIGNED_CLIENT_SECRET_FOR_OAUTH');
/**
* The URI of the standard bitly v3 API.
*/
define('bitly_api', 'http://api.bit.ly/v3/');
/**
* The URI of the bitly OAuth endpoints.
*/
define('bitly_oauth_api', 'https://api-ssl.bit.ly/v3/');
/**
* The URI for OAuth access token requests.
*/
define('bitly_oauth_access_token', 'https://api-ssl.bit.ly/oauth/');
/**
* Given a longUrl, get the bit.ly shortened version.
*
* Example usage:
* @code
* $results = bitly_v3_shorten('http://knowabout.it', 'j.mp');
* @endcode
*
* @param $longUrl
* Long URL to be shortened.
* @param $domain
* Uses bit.ly (default), j.mp, or a bit.ly pro domain.
* @param $x_login
* User's login name.
* @param $x_api_key
* User's API key.
*
* @return
* An associative array containing:
* - url: The unique shortened link that should be used, this is a unique
* value for the given bit.ly account.
* - hash: A bit.ly identifier for long_url which is unique to the given
* account.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - long_url: An echo back of the longUrl request parameter.
* - new_hash: Will be set to 1 if this is the first time this long_url was
* shortened by this user. It will also then be added to the user history.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/shorten
*/
function bitly_v3_shorten($longUrl, $domain = '', $x_login = '', $x_apiKey = '') {
$result = array();
$url = bitly_api . "shorten?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&longUrl=" . urlencode($longUrl);
if ($domain != '') {
$url .= "&domain=" . $domain;
}
if ($x_login != '' && $x_apiKey != '') {
$url .= "&x_login=" . $x_login . "&x_apiKey=" . $x_apiKey;
}
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'hash'})) {
$result['url'] = $output->{'data'}->{'url'};
$result['hash'] = $output->{'data'}->{'hash'};
$result['global_hash'] = $output->{'data'}->{'global_hash'};
$result['long_url'] = $output->{'data'}->{'long_url'};
$result['new_hash'] = $output->{'data'}->{'new_hash'};
}
return $result;
}
/**
* Expand a bit.ly url or hash.
*
* @param $data
* Either a full bit.ly short url or a bit.ly hash to be expanded.
*
* @return
* An associative array containing:
* - hash: A bit.ly identifier for long_url which is unique to the given
* account.
* - long_url: The URL that the requested short_url or hash points to.
* - user_hash: The corresponding bit.ly user identifier.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/expand
*/
function bitly_v3_expand($data) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$data = implode('&hash=', $recs);
} else {
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
}
// make the call to expand
$url = bitly_api . "expand?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'expand'})) {
foreach ($output->{'data'}->{'expand'} as $tmp) {
$rec = array();
$rec['hash'] = $tmp->{'hash'};
$rec['long_url'] = $tmp->{'long_url'};
$rec['user_hash'] = $tmp->{'user_hash'};
$rec['global_hash'] = $tmp->{'global_hash'};
array_push($results, $rec);
}
}
return $results;
}
/**
* Validate that a bit.ly login/apiKey combination is valid.
*
* @param $x_login
* The end users user's bit.ly login (for validation).
* @param $x_apiKey
* The end users bit.ly apiKey (for validation).
*
* @return
* TRUE if the combination is valid.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/validate
*/
function bitly_v3_validate($x_login, $x_apiKey) {
$result = 0;
$url = bitly_api . "validate?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&x_login=" . $x_login . "&x_apiKey=" . $x_apiKey;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'valid'})) {
$result = $output->{'data'}->{'valid'};
}
return (bool) $result;
}
/**
* For one or more bit.ly URL's or hashes, returns statistics about the clicks
* on that link.
*
* @param $data
* Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
* and/or hashes.
*
* @return
* A multidimensional numbered associative array containing:
* - short_url: The unique bit.ly hash.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - user_clicks: The total count of clicks to this user's bit.ly link.
* - user_hash: The corresponding bit.ly user identifier.
* - global_clicks: The total count of clicks to all bit.ly links that point
* to the same same long url.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks
*/
function bitly_v3_clicks($data) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$data = implode('&hash=', $recs);
} else {
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
}
$url = bitly_api . "clicks?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'clicks'})) {
foreach ($output->{'data'}->{'clicks'} as $tmp) {
$rec = array();
$rec['short_url'] = $tmp->{'short_url'};
$rec['global_hash'] = $tmp->{'global_hash'};
$rec['user_clicks'] = $tmp->{'user_clicks'};
$rec['user_hash'] = $tmp->{'user_hash'};
$rec['global_clicks'] = $tmp->{'global_clicks'};
array_push($results, $rec);
}
}
return $results;
}
/**
* Provides a list of referring sites for a specified bit.ly short link or hash,
* and the number of clicks per referrer.
*
* @param $data
* A bit.ly shortened URL or bit.ly hash.
*
* @return
* An associative array containing:
* - created_by: The service that created the link.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - short_url: The unique bit.ly hash.
* - user_hash: The corresponding bit.ly user identifier.
* - referrers: A multidimensional numbered associative array containing:
* - clicks: Number of clicks from this referrer.
* - referrer: (optional) Referring site.
* - referrer_app: (optional) Referring application (e.g.: Tweetdeck).
* - url: (optional) URL of referring application
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/referrers
*/
function bitly_v3_referrers($data) {
$results = array();
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
$url = bitly_api . "referrers?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'referrers'})) {
$results['created_by'] = $output->{'data'}->{'created_by'};
$results['global_hash'] = $output->{'data'}->{'global_hash'};
$results['short_url'] = $output->{'data'}->{'short_url'};
$results['user_hash'] = $output->{'data'}->{'user_hash'};
$results['referrers'] = array();
foreach ($output->{'data'}->{'referrers'} as $tmp) {
$rec = array();
$rec['clicks'] = $tmp->{'clicks'};
$rec['referrer'] = $tmp->{'referrer'};
$rec['referrer_app'] = $tmp->{'referrer_app'};
$rec['url'] = $tmp->{'url'};
array_push($results['referrers'], $rec);
}
}
return $results;
}
/**
* Provides a list of countries from which clicks on a specified bit.ly short
* link or hash have originated, and the number of clicks per country.
*
* @param $data
* A bit.ly shortened URL or bit.ly hash.
*
* @return
* An associative array containing:
* - created_by: The service that created the link.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - short_url: The unique bit.ly hash.
* - user_hash: The corresponding bit.ly user identifier.
* - countries: A multidimensional numbered associative array containing:
* - clicks: Number of clicks from this country.
* - country: The country code these clicks originated from or null when
* displaying clicks that could not be mapped to a specific country.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/countries
*/
function bitly_v3_countries($data) {
$results = array();
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
$url = bitly_api . "countries?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'countries'})) {
$results['created_by'] = $output->{'data'}->{'created_by'};
$results['global_hash'] = $output->{'data'}->{'global_hash'};
$results['short_url'] = $output->{'data'}->{'short_url'};
$results['user_hash'] = $output->{'data'}->{'user_hash'};
$results['countries'] = array();
foreach ($output->{'data'}->{'countries'} as $tmp) {
$rec = array();
$rec['clicks'] = $tmp->{'clicks'};
$rec['country'] = $tmp->{'country'};
array_push($results['countries'], $rec);
}
}
return $results;
}
/**
* For one or more bit.ly short urls or hashes, provides time series clicks per
* minute for the last hour in reverse chronological order (most recent to least
* recent).
*
* @param $data
* Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
* and/or hashes.
*
* @return
* A multidimensional numbered associative array containing:
* - clicks: An array with sixty entires, each for the number of clicks
* received for the given link that minute.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - short_url: The unique bit.ly hash.
* - user_hash: The corresponding bit.ly user identifier.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks_by_minute
*/
function bitly_v3_clicks_by_minute($data) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$data = implode('&hash=', $recs);
} else {
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
}
$url = bitly_api . "clicks_by_minute?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'clicks_by_minute'})) {
foreach ($output->{'data'}->{'clicks_by_minute'} as $tmp) {
$rec = array();
$rec['clicks'] = $tmp->{'clicks'};
$rec['global_hash'] = $tmp->{'global_hash'};
$rec['short_url'] = $tmp->{'short_url'};
$rec['user_hash'] = $tmp->{'user_hash'};
array_push($results, $rec);
}
}
return $results;
}
/**
* For one or more bit.ly short urls or hashes, provides time series clicks per
* day for the last 30 days in reverse chronological order (most recent to least
* recent).
*
* @param $data
* Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
* and/or hashes.
*
* @return
* A multidimensional numbered associative array containing:
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - short_url: The unique bit.ly hash.
* - user_hash: The corresponding bit.ly user identifier.
* - clicks: A multidimensional numbered associative array containing:
* - clicks: The number of clicks received for a given link that day.
* - day_start: A time code representing the start of the day for which
* click data is provided.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/clicks_by_day
*/
function bitly_v3_clicks_by_day($data, $days = 7) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$data = implode('&hash=', $recs);
} else {
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
}
$url = bitly_api . "clicks_by_day?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&days=" . $days . "&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'clicks_by_day'})) {
foreach ($output->{'data'}->{'clicks_by_day'} as $tmp) {
$rec = array();
$rec['global_hash'] = $tmp->{'global_hash'};
$rec['short_url'] = $tmp->{'short_url'};
$rec['user_hash'] = $tmp->{'user_hash'};
$rec['clicks'] = array();
$clicks = $tmp->{'clicks'};
foreach ($clicks as $click) {
$clickrec = array();
$clickrec['clicks'] = $click->{'clicks'};
$clickrec['day_start'] = $click->{'day_start'};
array_push($rec['clicks'], $clickrec);
}
array_push($results, $rec);
}
}
return $results;
}
/**
* 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.
*
* @param $domain
* The short domain to check.
*
* @return
* An associative array containing:
* - domain: An echo back of the request parameter.
* - bitly_pro_domain: 0 or 1 designating whether this is a current bitly.Pro
* domain.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/bitly_pro_domain
*/
function bitly_v3_bitly_pro_domain($domain) {
$result = array();
$url = bitly_api . "bitly_pro_domain?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&domain=" . $domain;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'bitly_pro_domain'})) {
$result['domain'] = $output->{'data'}->{'domain'};
$result['bitly_pro_domain'] = $output->{'data'}->{'bitly_pro_domain'};
}
return $result;
}
/**
* This is used to query for a bit.ly link based on a long URL.
*
* @param $data
* One or more long URLs to lookup.
*
* @return
* An associative array containing:
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - short_url: The unique shortened link that should be used, this is a
* unique value for the given bit.ly account.
* - url: An echo back of the url parameter.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/lookup
*/
function bitly_v3_lookup($data) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
array_push($recs, urlencode($rec));
}
$data = implode('&url=', $recs);
} else {
$data = urlencode($data);
}
$url = bitly_api . "lookup?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&url=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'lookup'})) {
foreach ($output->{'data'}->{'lookup'} as $tmp) {
$rec = array();
$rec['global_hash'] = $tmp->{'global_hash'};
$rec['short_url'] = $tmp->{'short_url'};
$rec['url'] = $tmp->{'url'};
array_push($results, $rec);
}
}
return $results;
}
/**
* This is used by applications to lookup a bit.ly API key for a user given a
* bit.ly username and password.
*
* @param $x_login
* Bit.ly username.
* @param $x_password
* Bit.ly password.
*
* @return
* An associative array containing:
* - successful: An indicator of weather or not the login and password
* combination is valid.
* - username: The corresponding bit.ly users username.
* - api_key: The corresponding bit.ly users API key.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/authenticate
*/
function bitly_v3_authenticate($x_login, $x_password) {
$result = array();
$url = bitly_api . "authenticate";
$params = array();
$params['login'] = bitlyLogin;
$params['apiKey'] = bitlyKey;
$params['format'] = "json";
$params['x_login'] = $x_login;
$params['x_password'] = $x_password;
$output = json_decode(bitly_post_curl($url, $params));
if (isset($output->{'data'}->{'authenticate'})) {
$result['successful'] = $output->{'data'}->{'authenticate'}->{'successful'};
$result['username'] = $output->{'data'}->{'authenticate'}->{'username'};
$result['api_key'] = $output->{'data'}->{'authenticate'}->{'api_key'};
}
return $result;
}
/**
* This is used to return the page title for a given bit.ly link.
*
* @param $data
* Can be a bit.ly shortened URL, a bit.ly hash, or an array of bit.ly URLs
* and/or hashes.
*
* @return
* A multidimensional numbered associative array containing:
* - created_by: The service that created the link.
* - global_hash: A bit.ly identifier for long_url which can be used to track
* aggregate stats across all matching bit.ly links.
* - hash: The unique bit.ly hash.
* - title: The HTML page title for the destination page (when available).
* - user_hash: The corresponding bit.ly user identifier.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/info
*/
function bitly_v3_info($data) {
$results = array();
if (is_array($data)) {
// we need to flatten this into one proper command
$recs = array();
foreach ($data as $rec) {
$tmp = explode('/', $rec);
$tmp = array_reverse($tmp);
array_push($recs, $tmp[0]);
}
$data = implode('&hash=', $recs);
} else {
$tmp = explode('/', $data);
$tmp = array_reverse($tmp);
$data = $tmp[0];
}
// make the call to expand
$url = bitly_api . "info?login=" . bitlyLogin . "&apiKey=" . bitlyKey . "&format=json&hash=" . $data;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'info'})) {
foreach ($output->{'data'}->{'info'} as $tmp) {
$rec = array();
$rec['created_by'] = $tmp->{'created_by'};
$rec['global_hash'] = $tmp->{'global_hash'};
$rec['hash'] = $tmp->{'hash'};
$rec['title'] = $tmp->{'title'};
$rec['user_hash'] = $tmp->{'user_hash'};
array_push($results, $rec);
}
}
return $results;
}
/**
* Returns an OAuth access token as well as API users for a given code.
*
* @param $code
* The OAuth verification code acquired via OAuth’s web authentication
* protocol.
* @param $redirect
* The page to which a user was redirected upon successfully authenticating.
*
* @return
* An associative array containing:
* - login: The corresponding bit.ly users username.
* - api_key: The corresponding bit.ly users API key.
* - access_token: The OAuth access token for specified user.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/oauth/access_token
*/
function bitly_oauth_access_token($code, $redirect) {
$results = array();
$url = bitly_oauth_access_token . "access_token";
$params = array();
$params['client_id'] = bitly_clientid;
$params['client_secret'] = bitly_secret;
$params['code'] = $code;
$params['redirect_uri'] = $redirect;
$output = bitly_post_curl($url, $params);
$parts = explode('&', $output);
foreach ($parts as $part) {
$bits = explode('=', $part);
$results[$bits[0]] = $bits[1];
}
return $results;
}
/**
* Provides the total clicks per day on a user’s bit.ly links.
*
* @param $access_token
* The OAuth access token for the user.
* @param $days
* An integer value for the number of days (counting backwards from the
* current day) from which to retrieve data (min:1, max:30, default:7).
*
* @return
* An associative array containing:
* - days: An echo of the dupplied days parameter.
* - total_clicks: The total number of clicks over the supplied period.
* - clicks: A multidimensional numbered associative array containing:
* - clicks: The number of clicks received for a given link that day.
* - day_start: A time code representing the start of the day for which
* click data is provided.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/clicks
*/
function bitly_v3_user_clicks($access_token, $days = 7) {
// $results = bitly_v3_user_clicks('BITLY_SUPPLIED_ACCESS_TOKEN');
$results = array();
$url = bitly_oauth_api . "user/clicks?access_token=" . $access_token . "&days=" . $days;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'clicks'})) {
$results['days'] = $output->{'data'}->{'days'};
$results['total_clicks'] = $output->{'data'}->{'total_clicks'};
$results['clicks'] = array();
foreach ($output->{'data'}->{'clicks'} as $clicks) {
$rec = array();
$rec['clicks'] = $clicks->{'clicks'};
$rec['day_start'] = $clicks->{'day_start'};
array_push($results['clicks'], $rec);
}
}
return $results;
}
/**
* Provides a list of referring sites for a specified bit.ly user, and the
* number of clicks per referrer.
*
* @param $access_token
* The OAuth access token for the user.
* @param $days
* An integer value for the number of days (counting backwards from the
* current day) from which to retrieve data (min:1, max:30, default:7).
*
* @return
* An associative array containing:
* - days: An echo of the dupplied days parameter.
* - referrers: A multidimensional numbered associative array containing:
* - clicks: Number of clicks from this referrer.
* - referrer: (optional) Referring site.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/referrers
*/
function bitly_v3_user_referrers($access_token, $days = 7) {
// $results = bitly_v3_user_referrers('BITLY_SUPPLIED_ACCESS_TOKEN');
$results = array();
$url = bitly_oauth_api . "user/referrers?access_token=" . $access_token . "&days=" . $days;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'referrers'})) {
$results['days'] = $output->{'data'}->{'days'};
$results['referrers'] = array();
foreach ($output->{'data'}->{'referrers'} as $referrers) {
$recs = array();
foreach ($referrers as $ref) {
$rec = array();
$rec['referrer'] = $ref->{'referrer'};
$rec['clicks'] = $ref->{'clicks'};
array_push($recs, $rec);
}
array_push($results['referrers'], $recs);
}
}
return $results;
}
/**
* Provides a list of referring sites for a specified bit.ly short link or hash,
* and the number of clicks per referrer.
*
* @param $access_token
* The OAuth access token for the user.
* @param $days
* An integer value for the number of days (counting backwards from the
* current day) from which to retrieve data (min:1, max:30, default:7).
*
* @return
* An associative array containing:
* - days: An echo of the dupplied days parameter.
* - referrers: A multidimensional numbered associative array containing:
* - clicks: Number of clicks from this referrer.
* - countries: (optional) Country code for where the clicks originated.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/countries
*/
function bitly_v3_user_countries($access_token, $days = 7) {
// $results = bitly_v3_user_countries('BITLY_SUPPLIED_ACCESS_TOKEN');
$results = array();
$url = bitly_oauth_api . "user/countries?access_token=" . $access_token . "&days=" . $days;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'countries'})) {
$results['days'] = $output->{'data'}->{'days'};
$results['countries'] = array();
foreach ($output->{'data'}->{'countries'} as $countries) {
$recs = array();
foreach ($countries as $country) {
$rec = array();
$rec['country'] = $country->{'country'};
$rec['clicks'] = $country->{'clicks'};
array_push($recs, $rec);
}
array_push($results['countries'], $recs);
}
}
return $results;
}
/**
* Provides a given user’s 100 most popular links based on click traffic in the
* past hour, and the number of clicks per link.
*
* @param $access_token
* The OAuth access token for the user.
*
* @return
* A multidimensional numbered associative array containing:
* - user_hash: The corresponding bit.ly user identifier.
* - clicks: Number of clicks on this link.
*
* @see http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/user/realtime_links
*/
function bitly_v3_user_realtime_links($access_token) {
// $results = bitly_v3_user_realtime_links('BITLY_SUPPLIED_ACCESS_TOKEN');
$results = array();
$url = bitly_oauth_api . "user/realtime_links?format=json&access_token=" . $access_token;
$output = json_decode(bitly_get_curl($url));
if (isset($output->{'data'}->{'realtime_links'})) {
foreach ($output->{'data'}->{'realtime_links'} as $realtime_links) {
$rec = array();
$rec['clicks'] = $realtime_links->{'clicks'};
$rec['user_hash'] = $realtime_links->{'user_hash'};
array_push($results, $rec);
}
}
return $results;
}
/**
* Make a GET call to the bit.ly API.
*
* @param $uri
* URI to call.
*/
function bitly_get_curl($uri) {
$output = "";
try {
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
} catch (Exception $e) {
}
return $output;
}
/**
* Make a POST call to the bit.ly API.
*
* @param $uri
* URI to call.
* @param $fields
* Array of fields to send.
*/
function bitly_post_curl($uri, $fields) {
$output = "";
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
try {
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
} catch (Exception $e) {
}
return $output;
}
?>