forked from jsocol/bitly-api-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitly.php
More file actions
1681 lines (1601 loc) · 53.4 KB
/
Bitly.php
File metadata and controls
1681 lines (1601 loc) · 53.4 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
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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Bitly API Client.
*
* LICENSE: Apache Software License v2.0, see LICENSE
*
* @package Bitly
* @copyright Copyright (c) 2013 Bitly Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0
* @version 0.1.0
* @link https://github.com/bitly/bitly-api-php
*/
/**
* Base class for Bitly client errors.
*/
class BitlyError extends Exception
{}
/**
* Raised when communicating with the API results in an error.
*/
class BitlyAPIError extends BitlyError
{}
/**
* Build a query string from an array of param => values. Handle
* multiple values correctly.
*
* PHP's built-in http_build_query does the insane thing of converting
* param names when given an array value, which is only correct for PHP
* servers. This builds correct query strings given a smaller set of
* allowable inputs. Only one level of nesting is OK, e.g.:
*
* $data = array(
* 'foo' => 'FOO',
* 'bar' => array('1', '2', '3')
* );
* echo build_params($data);
* // foo=FOO&bar=1&bar=2&bar=3
*
* @internal
*
* @param array $params
*
* @return string
*/
function build_query(Array $params) {
$ret = array();
$fmt = '%s=%s';
$separator = '&';
foreach ($params as $k => $v) {
if (is_array($v)) {
foreach ($v as $_v) {
array_push($ret, sprintf($fmt, $k, urlencode($_v)));
}
} else {
array_push($ret, sprintf($fmt, $k, urlencode($v)));
}
}
return implode($separator, $ret);
}
/**
* A possibly-authenticated connection to the Bitly API.
*/
class Bitly
{
/**
* The application's OAuth2 client ID.
*
* @type string|null
*/
public $clientId = null;
/**
* The application's OAuth2 client secret.
*
* @type string|null
*/
public $clientSecret = null;
/**
* The user or application's OAuth2 access token.
*
* @type string|null
*/
public $accessToken = null;
/**
* The API hostname.
*
* @type string
*/
public $apiUrl = 'https://api-ssl.bitly.com/';
/**
* The application's user agent.
*
* @type string|null
*/
public $userAgent = null;
/**
* Timeout for communicating with Bitly.
*
* @type int
*/
public $timeout = 4;
/**
* Timeout for connecting to Bitly.
*
* @type int
*/
public $connectTimeout = 2;
/**
* Create a new Bitly API client with the given credentials.
*
* @param string $clientId
* (Optional) The application's registered Bitly client ID.
* @param string $clientSecret
* (Optional) The application's registered Bitly client secret.
* @param string $accessToken
* (Optional) The application or current user's access token. Necessary
* for most API methods.
*
* @return Bitly
*
* @see http://dev.bitly.com/authentication.html
*/
public function __construct($clientId=null, $clientSecret=null,
$accessToken=null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->accessToken = $accessToken;
$this->userAgent = 'PHP/' . phpversion() . ' bitly_api/0.1.0';
}
/**
* Given an OAuth2 auth code, get an access token from Bitly.
*
* Exchanges an auth code for an access token, sets the access token on
* this client instance, and returns the token.
*
* @param string $code
* Authorization code from Bitly.
* @param string $redirectUri
* The URI to which the user was redirected after authenticating.
*
* @return string
*
* @see http://dev.bitly.com/authentication.html
*/
public function getAccessToken($code, $redirectUri)
{
$params = array('code' => $code, 'redirect_uri' => $redirectUri,
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret);
$result = $this->call('oauth/access_token', $params, true, false);
$data = array();
parse_str($result, $data);
$this->accessToken = $data['access_token'];
return $data['access_token'];
}
/**
* Expand a short URL or hash.
*
* One parameter must be specified. If both are specified, the short URL
* has precedence.
*
* @param string $shortUrl
* (Optional) A full bitly short URL, e.g. "http://bit.ly/1234"
* @param string $hash
* (Optional) The hash component of a bitly short URL, e.g. "1234"
*
* @return array
*
* @see http://dev.bitly.com/links.html#v3_expand
*/
public function expand($shortUrl=null, $hash=null)
{
if (!$shortUrl && !$hash) {
throw new BitlyError('shortUrl or hash must be specified');
}
$params = array();
if ($shortUrl) {
$params['shortUrl'] = $shortUrl;
}
elseif ($hash) {
$params['hash'] = $hash;
}
$results = $this->call('v3/expand', $params);
return $results['expand'][0];
}
/**
* Get info about a given short URL or hash.
*
* One parameter must be specified. if both are specified, the short URL
* has precedence.
*
* @param string $shortUrl
* (Optional) A full bitly short URL, e.g. "http://bit.ly/1234"
* @param string $hash
* (Optional) The hash component of a bitly short URL, e.g. "1234"
* @param $expandUser
* (Optional) Include extra information about the user.
*
* @return array
*
* @see http://dev.bitly.com/links.html#v3_info
*/
public function info($shortUrl=null, $hash=null, $expandUser=null)
{
if (!$shortUrl && !$hash) {
throw new BitlyError('shortUrl or hash must be specified');
}
if ($expandUser !== null) {
$params = array('expand_user' => $expandUser);
}
if ($shortUrl) {
$params['shortUrl'] = $shortUrl;
}
elseif ($hash) {
$params['hash'] = $hash;
}
$results = $this->call('v3/info', $params);
return $results['info'][0];
}
/**
* Look up a bitly link based on a long URL.
*
* @param string $url
* A long URL to look up.
*
* @return string
*
* @see http://dev.bitly.com/links.html#v3_link_lookup
*/
public function linkLookup($url)
{
$params = array('url' => $url);
$results = $this->call('v3/link/lookup', $params);
return $results['link_lookup'][0]['aggregate_link'];
}
/**
* Shorten a long URL.
*
* 'domain' is a preferred domain, but if the user has a custom short
* domain, the custom domain will always be used.
*
* @param string $longUrl
* A long URL to shorten.
* @param string $domain
* (Optional) A preferred domain (either 'bit.ly', 'j.mp', or 'bitly.com').
*
* @returns array
* An associative array containing:
* - 'new_hash' (bool) Whether this URL has been shortened before.
* - 'url' (string) The actual short URL.
* - 'hash' (string) A Bitly identifier which is unique to this account.
* - 'global_hash' (string) A Bitly identifier which can be tracked
* across accounts.
* - 'long_url' (string) An normalized echo back of the original long URL.
*
* @see http://dev.bitly.com/links.html#v3_shorten
*/
public function shorten($url, $domain=null)
{
$params = array('longUrl' => $url);
if ($domain !== null) {
$params['domain'] = $domain;
}
$results = $this->call('v3/shorten', $params);
$results['new_hash'] = ($results['new_hash'] == 1) ? true : false;
return $results;
}
/**
* Edit a saved link.
*
* Possible keys/values for $params are as on
* http://dev.bitly.com/links.html#v3_user_link_edit
*
* @param string $link A bitly link, e.g. 'http://bit.ly/1234'.
* @param array $params An array of optional values from above.
*
* @return string An echo back of the edited link.
*
* @see http://dev.bitly.com/links.html#v3_user_link_edit
*/
public function userLinkEdit($link, Array $params)
{
$keys = array_keys($params);
$params['edit'] = implode(',', $keys);
$params['link'] = $link;
$results = $this->call('v3/user/link_edit', $params);
return $results['link_edit'];
}
/**
* Look up a Bitly link shortened by the authenticated user.
*
* @param string $url A long URL to look up.
*
* @return array
* An associative array containing:
* - 'url' (string) An echo back of the long URL.
* - 'link' (string) The corresponding Bitly link.
* - 'aggregate_link' (string) The corresponding aggregate link.
*
* @see http://dev.bitly.com/links.html#v3_user_link_lookup
*/
public function userLinkLookup($url) {
$params = array('url' => $url);
$results = $this->call('v3/user/link_lookup', $params);
return $results['link_lookup'][0];
}
/**
* Save a Bitly link as the authenticated user.
*
* Possible keys/values for $params are as on
* http://dev.bitly.com/links.html#v3_user_link_save
*
* @param string $url A long URL to shorten and save.
* @param array $params An associative array of optional values.
*
* @return array
* An associative array containing:
* - 'link' (string) The Bitly link for the long URL.
* - 'aggregate_link' (string) The corresponding Bitly aggregate link.
* - 'new_link' (bool) Whether the user has saved this link before.
* - 'long_url' (string) A normalized echo back of the long URL.
*
* @see http://dev.bitly.com/links.html#v3_user_link_save
*/
public function userLinkSave($url, Array $params)
{
$params['longUrl'] = $url;
$results = $this->call('v3/user/link_save', $params);
$results = $results['link_save'];
$results['new_link'] = ($results['new_link'] == 1) ? true : false;
return $results;
}
/**
* Return a specified number of 'high-value' Bitly links.
*
* @param int $limit Maximum number of links to return.
*
* @return array
* A list of Bitly links.
*
* @see http://dev.bitly.com/data_apis.html#v3_highvalue
*/
public function highvalue($limit)
{
$params = array('limit' => $limit);
$results = $this->call('v3/highvalue', $params);
return $results['values'];
}
/**
* Search links receiving clicks.
*
* @param string $query A string to search for.
* @param int $limit
* (Optional) The maximum number of links to return (10).
* @param int $offset
* (Optional) The result to start with (0).
* @param string $lang
* (Optional) A two-letter ISO language code.
* @param string $cities
* (Optional) Limit to links active in this city (ordered like
* 'us-il-chicago').
* @param string $domain
* (Optional) Restrict results to this domain.
* @fields string[]
* (Optional) Which fields to return in the response.
*
* @return array
* An array of results, each result is an associative array and its
* contents will depend on 'fields'.
*
* @see http://dev.bitly.com/data_apis.html#v3_search
*/
public function search($query, $limit=10, $offset=0, $lang=null,
$cities=null, $domain=null, Array $fields=null)
{
$params = array('query' => $query, 'limit' => $limit,
'offset' => $offset);
if ($lang !== null) {
$params['lang'] = $lang;
}
if ($cities !== null) {
$param['cities'] = $cities;
}
if ($domain !== null) {
$params['domain'] = $domain;
}
if ($fields !== null) {
$params['fields'] = implode(',', $fields);
}
$results = $this->call('v3/search', $params);
return $results['results'];
}
/**
* Returns phrases that are receiving an uncharacteristically high volume
* of click traffic, and the individual links (hashes) driving traffic to
* pages containing these phrases.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_realtime_bursting_phrases
*/
public function realtimeBurstingPhrases() {
return $this->call('v3/realtime/bursting_phrases');
}
/**
* Returns phrases that are receiving a consistently high volume of click
* traffic, and the individual links (hashes) driving traffic to pages
* containing these phrases.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_realtime_hot_phrases
*/
public function realtimeHotPhrases() {
return $this->call('v3/realtime/hot_phrases');
}
/**
* Returns the click rate for content containing a specified phrase.
*
* @param string $phrase
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_realtime_clickrate
*/
public function realtimeClickrate($phrase) {
$params = array('phrase' => $phrase);
return $this->call('v3/realtime/clickrate', $params);
}
/**
* Returns metadata about a single bitly link.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_info
*/
public function linkInfo($link) {
$params = array('link' => $link);
return $this->call('v3/link/info', $params);
}
/**
* Returns the “main article” from the linked page, as determined by the
* content extractor, in either HTML or plain text format.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_content
*/
public function linkContent($link) {
$params = array('link' => $link);
return $this->call('v3/link/content', $params);
}
/**
* Returns the detected categories for a document, in descending order of
* confidence.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_category
*/
public function linkCategory($link) {
$params = array('link' => $link);
$results = $this->call('v3/link/category', $params);
return $results['categories'];
}
/**
* Returns the "social score" for a specified bitly link.
*
* Note that the social score are highly dependent upon activity (clicks)
* occurring on the bitly link. If there have not been clicks on a bitly
* link within the last 24 hours, it is possible a social score for that
* link does not exist.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_social
*/
public function linkSocial($link) {
$params = array('link' => $link);
$results = $this->call('v3/link/social', $params);
return $results['social_scores'];
}
/**
* Returns the significant locations for the bitly link.
*
* Note that locations are highly dependent upon activity (clicks)
* occurring on the bitly link. If there have not been clicks on a bitly
* link within the last 24 hours, it is possible that location data for
* that link does not exist.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_location
*/
public function linkLocation($link) {
$params = array('link' => $link);
$results = $this->call('v3/link/location', $params);
return $results['locations'];
}
/**
* Returns the significant languages for the Bitly link.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/data_apis.html#v3_link_language
*/
public function linkLanguage($link) {
$params = array('link' => $link);
$results = $this->call('v3/link/language', $params);
return $results['languages'];
}
/**
* Returns the number of clicks on a single Bitly link.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param bool $rollup (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_clicks
*/
public function linkClicks($link, $unit='day', $units=null,
$timezone='America/New_York', $rollup=null,
$limit=100, $unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit,
'timezone' => $timezone, 'limit' => $limit,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
if ($rollup !== null) {
$params['rollup'] = $rollup;
}
return $this->call('v3/link/clicks', $params);
}
/**
* Returns metrics about the countries referring click traffic to a Bitly
* link.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_countries
*/
public function linkCountries($link, $unit='day', $units=null,
$timezone='America/New_York', $limit=100,
$unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit,
'timezone' => $timezone, 'limit' => $limit,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
return $this->call('v3/link/countries', $params);
}
/**
* Returns users who have encoded this link.
*
* Optionally restricted to the requesting user's social graph.
*
* @param string $link A Bitly link.
* @param bool $myNetwork (Optional)
* @param int $limit (Optional)
* @param bool $expandUser (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_encoders
*/
public function linkEncoders($link, $myNetwork=null, $limit=10,
$expandUser=null)
{
$params = array('link' => $link, 'limit' => $limit);
if ($myNetwork !== null) {
$params['my_network'] = $myNetwork;
}
if ($expandUser !== null) {
$params['expand_user'] = $expandUser;
}
return $this->call('v3/link/encoders', $params);
}
/**
* Returns the number of users who have shortened a single Bitly link.
*
* @param string $link A Bitly link.
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_encoders_count
*/
public function linkEncodersCount($link)
{
$params = array('link' => $link);
return $this->call('v3/link/encoders_count', $params);
}
/**
* Returns metrics about the pages referring click traffic to a Bitly link.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_referrers
*/
public function linkReferrers($link, $unit='day', $units=null,
$timezone='America/New_York', $limit=100,
$unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit, 'limit' => $limit,
'timezone' => $timezone,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
return $this->call('v3/link/referrers', $params);
}
/**
* Returns metrics about the pages referring click traffic to a Bitly link,
* grouped by domain.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_referrers_by_domain
*/
public function linkReferrersByDomain($link, $unit='day', $units=null,
$timezone='America/New_York',
$limit=100, $unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit, 'limit' => $limit,
'timezone' => $timezone,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
return $this->call('v3/link/refererrs_by_domain', $params);
}
/**
* Returns metrics about the domains referring click traffic to a Bitly
* link.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_referring_domains
*/
public function linkReferringDomains($link, $unit='day', $units=null,
$timezone='America/New_York',
$limit=100, $unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit, 'limit' => $limit,
'timezone' => $timezone,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
return $this->call('v3/link/refererring_domains', $params);
}
/**
* Returns metrics about shares of a single Bitly link.
*
* @param string $link A Bitly link.
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param bool $rollup (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_link_shares
*/
public function linkShares($link, $unit='day', $units=null,
$timezone='America/New_York', $rollup=null,
$limit=100, $unitReferenceTs='now')
{
$params = array('link' => $link, 'unit' => $unit, 'limit' => $limit,
'timezone' => $timezone,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
if ($rollup !== null) {
$params['rollup'] = $rollup;
}
return $this->call('v3/link/shares', $params);
}
/**
* Return or update information about a user.
*
* If called with no arguments, will return information about the
* authenticated user.
*
* The $fullName argument is only valid for the authenticated user and will
* set the user's full name property.
*
* @param string $login (Optional)
* @param string $fullName (Optional)
*
* @return array
*
* @see http://dev.bitly.com/user_info.html#v3_user_info
*/
public function userInfo($login=null, $fullName=null)
{
$params = array();
if ($login !== null) {
$params['login'] = $login;
}
if ($fullName !== null) {
$params['full_name'] = $fullName;
}
return $this->call('v3/user/info', $params);
}
/**
* Returns entries from a user's link history in reverse chronological
* order.
*
* @param string $link (Optional)
* @param int $limit (Optional)
* @param int $offset (Optional)
* @param int $createdBefore (Optional)
* @param int $createdAfter (Optional)
* @param int $modifiedAfter (Optional)
* @param bool $expandClientId (Optional)
* @param string $archived (Optional) "on", "off" or "both".
* @param string $private (Optional) "on", "off" or "both".
* @param string $user (Optional)
*
* @return array
*
* @see http://dev.bitly.com/user_info.html#v3_user_link_history
*/
public function userLinkHistory($link=null, $limit=50, $offset=null,
$createdBefore=null, $createdAfter=null,
$modifiedAfter=null, $expandClientId=null,
$archived=null, $private=null, $user=null)
{
$params = array();
if ($link !== null) {
$params['link'] = $link;
}
if ($limit !== null) {
$params['limit'] == $limit;
}
if ($offset !== null) {
$params['offset'] = $offset;
}
if ($createdBefore !== null) {
$params['created_before'] = $createdBefore;
}
if ($createdAfter !== null) {
$params['created_after'] = $createdAfter;
}
if ($modifiedAfter !== null) {
$params['modified_after'] = $modifiedAfter;
}
if ($archived !== null) {
$params['archived'] = $archived;
}
if ($private !== null) {
$params['private'] = $private;
}
if ($user !== null) {
$params['user'] = $user;
}
return $this->call('v3/user/link_history', $params);
}
/**
* Returns entries from a user's network history in reverse chronological
* order.
*
* @param int $offset (Optional)
* @param bool $expandClientId (Optional)
* @param int $limit (Optional)
* @param bool $expandUser (Optional)
*
* @return array
*
* @see http://dev.bitly.com/user_info.html#v3_user_network_history
*/
public function userNetworkHistory($offset=null, $expandClientId=false,
$limit=20, $expandUser=null)
{
$params = array('expand_client_id' => $expandClientId,
'limit' => $limit);
if ($offset !== null) {
$params['offset'] = $offset;
}
if ($expandUser !== null) {
$params['expand_user'] = $expandUser;
}
return $this->call('v3/user/network_history', $params);
}
/**
* Returns a list of tracking domains a user has configured.
*
* @return array
*
* @see http://dev.bitly.com/user_info.html#v3_user_tracking_domain_list
*/
public function userTrackingDomainList()
{
$result = $this->call('v3/user/tracking_domain_list');
return $result['tracking_domains'];
}
/**
* Returns the aggregate number of clicks on all the authenticated user's
* Bitly links.
*
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param bool $rollup (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_user_clicks
*/
public function userClicks($unit='day', $units=null,
$timezone='America/New_York', $rollup=null,
$limit=100, $unitReferenceTs='now')
{
$params = array('unit' => $unit, 'timezone' => $timezone,
'limit' => $limit,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
if ($rollup !== null) {
$params['rollup'] = $rollup;
}
return $this->call('v3/user/clicks', $params);
}
/**
* Returns aggregate metrics about the countries referring clicks to all
* the authenticated user's Bitly links.
*
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param bool $rollup (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_user_countries
*/
public function userCountries($unit='day', $units=null,
$timezone='America/New_York', $rollup=null,
$limit=100, $unitReferenceTs='now')
{
$params = array('unit' => $unit, 'timezone' => $timezone,
'limit' => $limit,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
if ($rollup !== null) {
$params['rollup'] = $rollup;
}
return $this->call('v3/user/countries', $params);
}
/**
* Returns the authenticated user's most-clicked Bitly links.
*
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_user_popular_links
*/
public function userPopularLinks($unit='day', $units=null,
$timezone='America/New_York', $limit=100,
$unitReferenceTs='now')
{
$params = array('unit' => $unit, 'timezone' => $timezone,
'limit' => $limit,
'unit_reference_ts' => $unitReferenceTs);
if ($units !== null) {
$params['units'] = $units;
}
return $this->call('v3/user/popular_links', $params);
}
/**
* Returns aggregate metrics about the pages referring click traffic to all
* of the authenticated user's Bitly links.
*
* @param string $unit (Optional)
* @param int $units (Optional)
* @param string|int $timezone (Optional)
* @param bool $rollup (Optional)
* @param int $limit (Optional)
* @param string $unitReferenceTs (Optional)
*
* @return array
*
* @see http://dev.bitly.com/link_metrics.html#v3_user_referrers
*/