Twitter.php
32.5 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Service
* @subpackage Twitter
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Twitter.php 23877 2011-04-28 20:17:01Z ralph $
*/
/**
* @see Zend_Rest_Client
*/
require_once 'Zend/Rest/Client.php';
/**
* @see Zend_Rest_Client_Result
*/
require_once 'Zend/Rest/Client/Result.php';
/**
* @see Zend_Oauth_Consumer
*/
require_once 'Zend/Oauth/Consumer.php';
/**
* @category Zend
* @package Zend_Service
* @subpackage Twitter
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Service_Twitter extends Zend_Rest_Client
{
/**
* 246 is the current limit for a status message, 140 characters are displayed
* initially, with the remainder linked from the web UI or client. The limit is
* applied to a html encoded UTF-8 string (i.e. entities are counted in the limit
* which may appear unusual but is a security measure).
*
* This should be reviewed in the future...
*/
const STATUS_MAX_CHARACTERS = 246;
/**
* OAuth Endpoint
*/
const OAUTH_BASE_URI = 'http://twitter.com/oauth';
/**
* @var Zend_Http_CookieJar
*/
protected $_cookieJar;
/**
* Date format for 'since' strings
*
* @var string
*/
protected $_dateFormat = 'D, d M Y H:i:s T';
/**
* Username
*
* @var string
*/
protected $_username;
/**
* Current method type (for method proxying)
*
* @var string
*/
protected $_methodType;
/**
* Zend_Oauth Consumer
*
* @var Zend_Oauth_Consumer
*/
protected $_oauthConsumer = null;
/**
* Types of API methods
*
* @var array
*/
protected $_methodTypes = array(
'status',
'user',
'directMessage',
'friendship',
'account',
'favorite',
'block'
);
/**
* Options passed to constructor
*
* @var array
*/
protected $_options = array();
/**
* Local HTTP Client cloned from statically set client
*
* @var Zend_Http_Client
*/
protected $_localHttpClient = null;
/**
* Constructor
*
* @param array $options Optional options array
* @return void
*/
public function __construct($options = null, Zend_Oauth_Consumer $consumer = null)
{
$this->setUri('http://api.twitter.com');
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (!is_array($options)) {
$options = array();
}
$options['siteUrl'] = self::OAUTH_BASE_URI;
$this->_options = $options;
if (isset($options['username'])) {
$this->setUsername($options['username']);
}
if (isset($options['accessToken'])
&& $options['accessToken'] instanceof Zend_Oauth_Token_Access) {
$this->setLocalHttpClient($options['accessToken']->getHttpClient($options));
} else {
$this->setLocalHttpClient(clone self::getHttpClient());
if ($consumer === null) {
$this->_oauthConsumer = new Zend_Oauth_Consumer($options);
} else {
$this->_oauthConsumer = $consumer;
}
}
}
/**
* Set local HTTP client as distinct from the static HTTP client
* as inherited from Zend_Rest_Client.
*
* @param Zend_Http_Client $client
* @return self
*/
public function setLocalHttpClient(Zend_Http_Client $client)
{
$this->_localHttpClient = $client;
$this->_localHttpClient->setHeaders('Accept-Charset', 'ISO-8859-1,utf-8');
return $this;
}
/**
* Get the local HTTP client as distinct from the static HTTP client
* inherited from Zend_Rest_Client
*
* @return Zend_Http_Client
*/
public function getLocalHttpClient()
{
return $this->_localHttpClient;
}
/**
* Checks for an authorised state
*
* @return bool
*/
public function isAuthorised()
{
if ($this->getLocalHttpClient() instanceof Zend_Oauth_Client) {
return true;
}
return false;
}
/**
* Retrieve username
*
* @return string
*/
public function getUsername()
{
return $this->_username;
}
/**
* Set username
*
* @param string $value
* @return Zend_Service_Twitter
*/
public function setUsername($value)
{
$this->_username = $value;
return $this;
}
/**
* Proxy service methods
*
* @param string $type
* @return Zend_Service_Twitter
* @throws Zend_Service_Twitter_Exception If method not in method types list
*/
public function __get($type)
{
if (!in_array($type, $this->_methodTypes)) {
include_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Invalid method type "' . $type . '"'
);
}
$this->_methodType = $type;
return $this;
}
/**
* Method overloading
*
* @param string $method
* @param array $params
* @return mixed
* @throws Zend_Service_Twitter_Exception if unable to find method
*/
public function __call($method, $params)
{
if (method_exists($this->_oauthConsumer, $method)) {
$return = call_user_func_array(array($this->_oauthConsumer, $method), $params);
if ($return instanceof Zend_Oauth_Token_Access) {
$this->setLocalHttpClient($return->getHttpClient($this->_options));
}
return $return;
}
if (empty($this->_methodType)) {
include_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Invalid method "' . $method . '"'
);
}
$test = $this->_methodType . ucfirst($method);
if (!method_exists($this, $test)) {
include_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Invalid method "' . $test . '"'
);
}
return call_user_func_array(array($this, $test), $params);
}
/**
* Initialize HTTP authentication
*
* @return void
*/
protected function _init()
{
if (!$this->isAuthorised() && $this->getUsername() !== null) {
require_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Twitter session is unauthorised. You need to initialize '
. 'Zend_Service_Twitter with an OAuth Access Token or use '
. 'its OAuth functionality to obtain an Access Token before '
. 'attempting any API actions that require authorisation'
);
}
$client = $this->_localHttpClient;
$client->resetParameters();
if (null == $this->_cookieJar) {
$client->setCookieJar();
$this->_cookieJar = $client->getCookieJar();
} else {
$client->setCookieJar($this->_cookieJar);
}
}
/**
* Set date header
*
* @param int|string $value
* @deprecated Not supported by Twitter since April 08, 2009
* @return void
*/
protected function _setDate($value)
{
if (is_int($value)) {
$date = date($this->_dateFormat, $value);
} else {
$date = date($this->_dateFormat, strtotime($value));
}
$this->_localHttpClient->setHeaders('If-Modified-Since', $date);
}
/**
* Public Timeline status
*
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function statusPublicTimeline()
{
$this->_init();
$path = '/1/statuses/public_timeline.xml';
$response = $this->_get($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Friend Timeline Status
*
* $params may include one or more of the following keys
* - id: ID of a friend whose timeline you wish to receive
* - count: how many statuses to return
* - since_id: return results only after the specific tweet
* - page: return page X of results
*
* @param array $params
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return void
*/
public function statusFriendsTimeline(array $params = array())
{
$this->_init();
$path = '/1/statuses/friends_timeline';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'count':
$count = (int) $value;
if (0 >= $count) {
$count = 1;
} elseif (200 < $count) {
$count = 200;
}
$_params['count'] = (int) $count;
break;
case 'since_id':
$_params['since_id'] = $this->_validInteger($value);
break;
case 'page':
$_params['page'] = (int) $value;
break;
case 'max_id':
$_params['max_id'] = $this->_validInteger($value);
break;
case 'include_rts':
case 'trim_user':
case 'include_entities':
$_params[strtolower($key)] = $value ? '1' : '0';
break;
default:
break;
}
}
$path .= '.xml';
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* User Timeline status
*
* $params may include one or more of the following keys
* - id: ID of a friend whose timeline you wish to receive
* - since_id: return results only after the tweet id specified
* - page: return page X of results
* - count: how many statuses to return
* - max_id: returns only statuses with an ID less than or equal to the specified ID
* - user_id: specifies the ID of the user for whom to return the user_timeline
* - screen_name: specfies the screen name of the user for whom to return the user_timeline
* - include_rts: whether or not to return retweets
* - trim_user: whether to return just the user ID or a full user object; omit to return full object
* - include_entities: whether or not to return entities nodes with tweet metadata
*
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function statusUserTimeline(array $params = array())
{
$this->_init();
$path = '/1/statuses/user_timeline';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'id':
$path .= '/' . $value;
break;
case 'page':
$_params['page'] = (int) $value;
break;
case 'count':
$count = (int) $value;
if (0 >= $count) {
$count = 1;
} elseif (200 < $count) {
$count = 200;
}
$_params['count'] = $count;
break;
case 'user_id':
$_params['user_id'] = $this->_validInteger($value);
break;
case 'screen_name':
$_params['screen_name'] = $this->_validateScreenName($value);
break;
case 'since_id':
$_params['since_id'] = $this->_validInteger($value);
break;
case 'max_id':
$_params['max_id'] = $this->_validInteger($value);
break;
case 'include_rts':
case 'trim_user':
case 'include_entities':
$_params[strtolower($key)] = $value ? '1' : '0';
break;
default:
break;
}
}
$path .= '.xml';
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Show a single status
*
* @param int $id Id of status to show
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function statusShow($id)
{
$this->_init();
$path = '/1/statuses/show/' . $this->_validInteger($id) . '.xml';
$response = $this->_get($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Update user's current status
*
* @param string $status
* @param int $in_reply_to_status_id
* @return Zend_Rest_Client_Result
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @throws Zend_Service_Twitter_Exception if message is too short or too long
*/
public function statusUpdate($status, $inReplyToStatusId = null)
{
$this->_init();
$path = '/1/statuses/update.xml';
$len = iconv_strlen(htmlspecialchars($status, ENT_QUOTES, 'UTF-8'), 'UTF-8');
if ($len > self::STATUS_MAX_CHARACTERS) {
include_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Status must be no more than '
. self::STATUS_MAX_CHARACTERS
. ' characters in length'
);
} elseif (0 == $len) {
include_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Status must contain at least one character'
);
}
$data = array('status' => $status);
if (is_numeric($inReplyToStatusId) && !empty($inReplyToStatusId)) {
$data['in_reply_to_status_id'] = $inReplyToStatusId;
}
$response = $this->_post($path, $data);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Get status replies
*
* $params may include one or more of the following keys
* - since_id: return results only after the specified tweet id
* - page: return page X of results
*
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function statusReplies(array $params = array())
{
$this->_init();
$path = '/1/statuses/mentions.xml';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'since_id':
$_params['since_id'] = $this->_validInteger($value);
break;
case 'page':
$_params['page'] = (int) $value;
break;
default:
break;
}
}
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Destroy a status message
*
* @param int $id ID of status to destroy
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function statusDestroy($id)
{
$this->_init();
$path = '/1/statuses/destroy/' . $this->_validInteger($id) . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* User friends
*
* @param int|string $id Id or username of user for whom to fetch friends
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function userFriends(array $params = array())
{
$this->_init();
$path = '/1/statuses/friends';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'id':
$path .= '/' . $value;
break;
case 'page':
$_params['page'] = (int) $value;
break;
default:
break;
}
}
$path .= '.xml';
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* User Followers
*
* @param bool $lite If true, prevents inline inclusion of current status for followers; defaults to false
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function userFollowers($lite = false)
{
$this->_init();
$path = '/1/statuses/followers.xml';
if ($lite) {
$this->lite = 'true';
}
$response = $this->_get($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Show extended information on a user
*
* @param int|string $id User ID or name
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function userShow($id)
{
$this->_init();
$path = '/1/users/show.xml';
$response = $this->_get($path, array('id'=>$id));
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Retrieve direct messages for the current user
*
* $params may include one or more of the following keys
* - since_id: return statuses only greater than the one specified
* - page: return page X of results
*
* @param array $params
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function directMessageMessages(array $params = array())
{
$this->_init();
$path = '/1/direct_messages.xml';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'since_id':
$_params['since_id'] = $this->_validInteger($value);
break;
case 'page':
$_params['page'] = (int) $value;
break;
default:
break;
}
}
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Retrieve list of direct messages sent by current user
*
* $params may include one or more of the following keys
* - since_id: return statuses only greater than the one specified
* - page: return page X of results
*
* @param array $params
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function directMessageSent(array $params = array())
{
$this->_init();
$path = '/1/direct_messages/sent.xml';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'since_id':
$_params['since_id'] = $this->_validInteger($value);
break;
case 'page':
$_params['page'] = (int) $value;
break;
default:
break;
}
}
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Send a direct message to a user
*
* @param int|string $user User to whom to send message
* @param string $text Message to send to user
* @return Zend_Rest_Client_Result
* @throws Zend_Service_Twitter_Exception if message is too short or too long
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
*/
public function directMessageNew($user, $text)
{
$this->_init();
$path = '/1/direct_messages/new.xml';
$len = iconv_strlen($text, 'UTF-8');
if (0 == $len) {
throw new Zend_Service_Twitter_Exception(
'Direct message must contain at least one character'
);
} elseif (140 < $len) {
throw new Zend_Service_Twitter_Exception(
'Direct message must contain no more than 140 characters'
);
}
$data = array('user' => $user, 'text' => $text);
$response = $this->_post($path, $data);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Destroy a direct message
*
* @param int $id ID of message to destroy
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function directMessageDestroy($id)
{
$this->_init();
$path = '/1/direct_messages/destroy/' . $this->_validInteger($id) . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Create friendship
*
* @param int|string $id User ID or name of new friend
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function friendshipCreate($id)
{
$this->_init();
$path = '/1/friendships/create/' . $id . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Destroy friendship
*
* @param int|string $id User ID or name of friend to remove
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function friendshipDestroy($id)
{
$this->_init();
$path = '/1/friendships/destroy/' . $id . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Friendship exists
*
* @param int|string $id User ID or name of friend to see if they are your friend
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_result
*/
public function friendshipExists($id)
{
$this->_init();
$path = '/1/friendships/exists.xml';
$data = array('user_a' => $this->getUsername(), 'user_b' => $id);
$response = $this->_get($path, $data);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Verify Account Credentials
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
*
* @return Zend_Rest_Client_Result
*/
public function accountVerifyCredentials()
{
$this->_init();
$response = $this->_get('/1/account/verify_credentials.xml');
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* End current session
*
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return true
*/
public function accountEndSession()
{
$this->_init();
$this->_get('/1/account/end_session');
return true;
}
/**
* Returns the number of api requests you have left per hour.
*
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function accountRateLimitStatus()
{
$this->_init();
$response = $this->_get('/1/account/rate_limit_status.xml');
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Fetch favorites
*
* $params may contain one or more of the following:
* - 'id': Id of a user for whom to fetch favorites
* - 'page': Retrieve a different page of resuls
*
* @param array $params
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function favoriteFavorites(array $params = array())
{
$this->_init();
$path = '/1/favorites';
$_params = array();
foreach ($params as $key => $value) {
switch (strtolower($key)) {
case 'id':
$path .= '/' . $this->_validInteger($value);
break;
case 'page':
$_params['page'] = (int) $value;
break;
default:
break;
}
}
$path .= '.xml';
$response = $this->_get($path, $_params);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Mark a status as a favorite
*
* @param int $id Status ID you want to mark as a favorite
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function favoriteCreate($id)
{
$this->_init();
$path = '/1/favorites/create/' . $this->_validInteger($id) . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Remove a favorite
*
* @param int $id Status ID you want to de-list as a favorite
* @throws Zend_Http_Client_Exception if HTTP request fails or times out
* @return Zend_Rest_Client_Result
*/
public function favoriteDestroy($id)
{
$this->_init();
$path = '/1/favorites/destroy/' . $this->_validInteger($id) . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Blocks the user specified in the ID parameter as the authenticating user.
* Destroys a friendship to the blocked user if it exists.
*
* @param integer|string $id The ID or screen name of a user to block.
* @return Zend_Rest_Client_Result
*/
public function blockCreate($id)
{
$this->_init();
$path = '/1/blocks/create/' . $id . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Un-blocks the user specified in the ID parameter for the authenticating user
*
* @param integer|string $id The ID or screen_name of the user to un-block.
* @return Zend_Rest_Client_Result
*/
public function blockDestroy($id)
{
$this->_init();
$path = '/1/blocks/destroy/' . $id . '.xml';
$response = $this->_post($path);
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Returns if the authenticating user is blocking a target user.
*
* @param string|integer $id The ID or screen_name of the potentially blocked user.
* @param boolean $returnResult Instead of returning a boolean return the rest response from twitter
* @return Boolean|Zend_Rest_Client_Result
*/
public function blockExists($id, $returnResult = false)
{
$this->_init();
$path = '/1/blocks/exists/' . $id . '.xml';
$response = $this->_get($path);
$cr = new Zend_Rest_Client_Result($response->getBody());
if ($returnResult === true)
return $cr;
if (!empty($cr->request)) {
return false;
}
return true;
}
/**
* Returns an array of user objects that the authenticating user is blocking
*
* @param integer $page Optional. Specifies the page number of the results beginning at 1. A single page contains 20 ids.
* @param boolean $returnUserIds Optional. Returns only the userid's instead of the whole user object
* @return Zend_Rest_Client_Result
*/
public function blockBlocking($page = 1, $returnUserIds = false)
{
$this->_init();
$path = '/1/blocks/blocking';
if ($returnUserIds === true) {
$path .= '/ids';
}
$path .= '.xml';
$response = $this->_get($path, array('page' => $page));
return new Zend_Rest_Client_Result($response->getBody());
}
/**
* Protected function to validate that the integer is valid or return a 0
* @param mixed $int
* @return integer
*/
protected function _validInteger($int)
{
if (preg_match("/(\d+)/", $int)) {
return $int;
}
return 0;
}
/**
* Validate a screen name using Twitter rules
*
* @param string $name
* @throws Zend_Service_Twitter_Exception
* @return string
*/
protected function _validateScreenName($name)
{
if (!preg_match('/^[a-zA-Z0-9_]{0,15}$/', $name)) {
require_once 'Zend/Service/Twitter/Exception.php';
throw new Zend_Service_Twitter_Exception(
'Screen name, "' . $name
. '" should only contain alphanumeric characters and'
. ' underscores, and not exceed 15 characters.');
}
return $name;
}
/**
* Call a remote REST web service URI and return the Zend_Http_Response object
*
* @param string $path The path to append to the URI
* @throws Zend_Rest_Client_Exception
* @return void
*/
protected function _prepare($path)
{
// Get the URI object and configure it
if (!$this->_uri instanceof Zend_Uri_Http) {
require_once 'Zend/Rest/Client/Exception.php';
throw new Zend_Rest_Client_Exception(
'URI object must be set before performing call'
);
}
$uri = $this->_uri->getUri();
if ($path[0] != '/' && $uri[strlen($uri) - 1] != '/') {
$path = '/' . $path;
}
$this->_uri->setPath($path);
/**
* Get the HTTP client and configure it for the endpoint URI.
* Do this each time because the Zend_Http_Client instance is shared
* among all Zend_Service_Abstract subclasses.
*/
$this->_localHttpClient->resetParameters()->setUri((string) $this->_uri);
}
/**
* Performs an HTTP GET request to the $path.
*
* @param string $path
* @param array $query Array of GET parameters
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
protected function _get($path, array $query = null)
{
$this->_prepare($path);
$this->_localHttpClient->setParameterGet($query);
return $this->_localHttpClient->request(Zend_Http_Client::GET);
}
/**
* Performs an HTTP POST request to $path.
*
* @param string $path
* @param mixed $data Raw data to send
* @throws Zend_Http_Client_Exception
* @return Zend_Http_Response
*/
protected function _post($path, $data = null)
{
$this->_prepare($path);
return $this->_performPost(Zend_Http_Client::POST, $data);
}
/**
* Perform a POST or PUT
*
* Performs a POST or PUT request. Any data provided is set in the HTTP
* client. String data is pushed in as raw POST data; array or object data
* is pushed in as POST parameters.
*
* @param mixed $method
* @param mixed $data
* @return Zend_Http_Response
*/
protected function _performPost($method, $data = null)
{
$client = $this->_localHttpClient;
if (is_string($data)) {
$client->setRawData($data);
} elseif (is_array($data) || is_object($data)) {
$client->setParameterPost((array) $data);
}
return $client->request($method);
}
}