1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.service.twitter" xmlns:xi="http://www.w3.org/2001/XInclude">
4 <title>Zend_Service_Twitter</title>
6 <sect2 id="zend.service.twitter.introduction">
7 <title>Introduction</title>
10 <classname>Zend_Service_Twitter</classname> provides a client for the
11 <ulink url="http://apiwiki.twitter.com/Twitter-API-Documentation">Twitter
12 <acronym>REST</acronym> <acronym>API</acronym></ulink>.
13 <classname>Zend_Service_Twitter</classname> allows you to query the public timeline. If
14 you provide a username and password for Twitter, it will allow you to get and update
15 your status, reply to friends, direct message friends, mark tweets as favorite, and
20 <classname>Zend_Service_Twitter</classname> is implementing a <acronym>REST</acronym>
21 service, and all methods return an instance of
22 <classname>Zend_Rest_Client_Result</classname>.
26 <classname>Zend_Service_Twitter</classname> is broken up into subsections so you can
27 easily identify which type of call is being requested.
33 <code>account</code> makes sure that your account credentials are valid, checks
34 your <acronym>API</acronym> rate limit, and ends the current session for the
41 <code>status</code> retrieves the public and user timelines and
42 shows, updates, destroys, and retrieves replies for the authenticated user.
48 <code>user</code> retrieves friends and followers for the authenticated user
49 and returns extended information about a passed user.
55 <code>directMessage</code> retrieves the authenticated user's received direct
56 messages, deletes direct messages, and sends new direct messages.
62 <code>friendship</code> creates and removes friendships for the
69 <code>favorite</code> lists, creates, and removes favorite tweets.
75 <code>block</code> blocks and unblocks users from following you.
81 <sect2 id="zend.service.twitter.authentication">
82 <title>Authentication</title>
85 With the exception of fetching the public timeline,
86 <classname>Zend_Service_Twitter</classname> requires authentication to work.
87 Twitter currently uses
88 <ulink url="http://en.wikipedia.org/wiki/Basic_authentication_scheme">HTTP Basic
89 Authentication</ulink>. You can pass in your username or registered email along with
90 your password for Twitter to login.
93 <example id="zend.service.twitter.authentication.example">
94 <title>Creating the Twitter Class</title>
97 The following code sample is how you create the Twitter service, pass in your
98 username and password, and verify that they are correct.
101 <programlisting language="php"><![CDATA[
102 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
104 // verify your credentials with Twitter
105 $response = $twitter->account->verifyCredentials();
109 You can also pass in an array that contains the username and password as the
113 <programlisting language="php"><![CDATA[
114 $userInfo = array('username' => 'foo', 'password' => 'bar');
115 $twitter = new Zend_Service_Twitter($userInfo);
117 // verify your credentials with Twitter
118 $response = $twitter->account->verifyCredentials();
123 <sect2 id="zend.service.twitter.account">
124 <title>Account Methods</title>
129 <methodname>verifyCredentials()</methodname> tests if supplied user
130 credentials are valid with minimal overhead.
133 <example id="zend.service.twitter.account.verifycredentails">
134 <title>Verifying credentials</title>
136 <programlisting language="php"><![CDATA[
137 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
138 $response = $twitter->account->verifyCredentials();
145 <methodname>endSession()</methodname> signs users out of
146 client-facing applications.
149 <example id="zend.service.twitter.account.endsession">
150 <title>Sessions ending</title>
152 <programlisting language="php"><![CDATA[
153 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
154 $response = $twitter->account->endSession();
161 <methodname>rateLimitStatus()</methodname> returns the remaining number of
162 <acronym>API</acronym> requests available to the authenticating user
163 before the <acronym>API</acronym> limit is reached for the current hour.
166 <example id="zend.service.twitter.account.ratelimitstatus">
167 <title>Rating limit status</title>
169 <programlisting language="php"><![CDATA[
170 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
171 $response = $twitter->account->rateLimitStatus();
178 <sect2 id="zend.service.twitter.status">
179 <title>Status Methods</title>
184 <methodname>publicTimeline()</methodname> returns the 20 most recent statuses
185 from non-protected users with a custom user icon. The public timeline is cached
186 by Twitter for 60 seconds.
189 <example id="zend.service.twitter.status.publictimeline">
190 <title>Retrieving public timeline</title>
192 <programlisting language="php"><![CDATA[
193 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
194 $response = $twitter->status->publicTimeline();
201 <methodname>friendsTimeline()</methodname> returns the 20 most recent statuses
202 posted by the authenticating user and that user's friends.
205 <example id="zend.service.twitter.status.friendstimeline">
206 <title>Retrieving friends timeline</title>
208 <programlisting language="php"><![CDATA[
209 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
210 $response = $twitter->status->friendsTimeline();
215 The <methodname>friendsTimeline()</methodname> method accepts an array of
216 optional parameters to modify the query.
222 <code>since</code> narrows the returned results to just those statuses
223 created after the specified date/time (up to 24 hours old).
229 <code>page</code> specifies which page you want to return.
237 <methodname>userTimeline()</methodname> returns the 20 most recent statuses
238 posted from the authenticating user.
241 <example id="zend.service.twitter.status.usertimeline">
242 <title>Retrieving user timeline</title>
244 <programlisting language="php"><![CDATA[
245 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
246 $response = $twitter->status->userTimeline();
251 The <methodname>userTimeline()</methodname> method accepts an array of optional
252 parameters to modify the query.
258 <code>id</code> specifies the ID or screen name of the user for whom to
259 return the friends_timeline.
265 <code>since</code> narrows the returned results to just those statuses
266 created after the specified date/time (up to 24 hours old).
272 <code>page</code> specifies which page you want to return.
278 <code>count</code> specifies the number of statuses to retrieve.
279 May not be greater than 200.
287 <methodname>show()</methodname> returns a single status, specified by the
288 <code>id</code> parameter below. The status' author will be returned inline.
291 <example id="zend.service.twitter.status.show">
292 <title>Showing user status</title>
294 <programlisting language="php"><![CDATA[
295 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
296 $response = $twitter->status->show(1234);
303 <methodname>update()</methodname> updates the authenticating user's status.
304 This method requires that you pass in the status update that you want to post
308 <example id="zend.service.twitter.status.update">
309 <title>Updating user status</title>
311 <programlisting language="php"><![CDATA[
312 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
313 $response = $twitter->status->update('My Great Tweet');
318 The <methodname>update()</methodname> method accepts a second additional
325 <code>in_reply_to_status_id</code> specifies the ID of an existing
326 status that the status to be posted is in reply to.
334 <methodname>replies()</methodname> returns the 20 most recent @replies (status
335 updates prefixed with @username) for the authenticating user.
338 <example id="zend.service.twitter.status.replies">
339 <title>Showing user replies</title>
341 <programlisting language="php"><![CDATA[
342 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
343 $response = $twitter->status->replies();
348 The <methodname>replies()</methodname> method accepts an array of optional
349 parameters to modify the query.
355 <code>since</code> narrows the returned results to just those statuses
356 created after the specified date/time (up to 24 hours old).
362 <code>page</code> specifies which page you want to return.
367 <code>since_id</code> returns only statuses with an ID greater than
368 (that is, more recent than) the specified ID.
376 <methodname>destroy()</methodname> destroys the status specified by the
377 required <code>id</code> parameter.
380 <example id="zend.service.twitter.status.destroy">
381 <title>Deleting user status</title>
383 <programlisting language="php"><![CDATA[
384 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
385 $response = $twitter->status->destroy(12345);
392 <sect2 id="zend.service.twitter.user">
393 <title>User Methods</title>
398 <methodname>friends()</methodname>r eturns up to 100 of the authenticating
399 user's friends who have most recently updated, each with current status inline.
402 <example id="zend.service.twitter.user.friends">
403 <title>Retrieving user friends</title>
405 <programlisting language="php"><![CDATA[
406 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
407 $response = $twitter->user->friends();
412 The <methodname>friends()</methodname> method accepts an array of optional
413 parameters to modify the query.
419 <code>id</code> specifies the ID or screen name of the user for whom to
420 return a list of friends.
426 <code>since</code> narrows the returned results to just those statuses
427 created after the specified date/time (up to 24 hours old).
433 <code>page</code> specifies which page you want to return.
441 <methodname>followers()</methodname> returns the authenticating user's
442 followers, each with current status inline.
445 <example id="zend.service.twitter.user.followers">
446 <title>Retrieving user followers</title>
448 <programlisting language="php"><![CDATA[
449 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
450 $response = $twitter->user->followers();
455 The <methodname>followers()</methodname> method accepts an array of optional
456 parameters to modify the query.
462 <code>id</code> specifies the ID or screen name of the user for whom to
463 return a list of followers.
469 <code>page</code> specifies which page you want to return.
477 <methodname>show()</methodname> returns extended information of a given user,
478 specified by ID or screen name as per the required <code>id</code>
482 <example id="zend.service.twitter.user.show">
483 <title>Showing user informations</title>
485 <programlisting language="php"><![CDATA[
486 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
487 $response = $twitter->user->show('myfriend');
494 <sect2 id="zend.service.twitter.directmessage">
495 <title>Direct Message Methods</title>
500 <methodname>messages()</methodname> returns a list of the 20 most recent direct
501 messages sent to the authenticating user.
504 <example id="zend.service.twitter.directmessage.messages">
505 <title>Retrieving recent direct messages received</title>
507 <programlisting language="php"><![CDATA[
508 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
509 $response = $twitter->directMessage->messages();
514 The <methodname>message()</methodname> method accepts an array of optional
515 parameters to modify the query.
521 <code>since_id</code> returns only direct messages with an ID greater
522 than (that is, more recent than) the specified ID.
528 <code>since</code> narrows the returned results to just those statuses
529 created after the specified date/time (up to 24 hours old).
535 <code>page</code> specifies which page you want to return.
543 <methodname>sent()</methodname> returns a list of the 20 most recent direct
544 messages sent by the authenticating user.
547 <example id="zend.service.twitter.directmessage.sent">
548 <title>Retrieving recent direct messages sent</title>
550 <programlisting language="php"><![CDATA[
551 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
552 $response = $twitter->directMessage->sent();
557 The <methodname>sent()</methodname> method accepts an array of optional
558 parameters to modify the query.
564 <code>since_id</code> returns only direct messages with an ID greater
565 than (that is, more recent than) the specified ID.
571 <code>since</code> narrows the returned results to just those statuses
572 created after the specified date/time (up to 24 hours old).
578 <code>page</code> specifies which page you want to return.
586 <methodname>new()</methodname> sends a new direct message to the specified user
587 from the authenticating user. Requires both the user and text parameters below.
590 <example id="zend.service.twitter.directmessage.new">
591 <title>Sending direct message</title>
593 <programlisting language="php"><![CDATA[
594 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
595 $response = $twitter->directMessage->new('myfriend', 'mymessage');
602 <methodname>destroy()</methodname> destroys the direct message specified in the
603 required <code>id</code> parameter. The authenticating user must be the
604 recipient of the specified direct message.
607 <example id="zend.service.twitter.directmessage.destroy">
608 <title>Deleting direct message</title>
610 <programlisting language="php"><![CDATA[
611 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
612 $response = $twitter->directMessage->destroy(123548);
619 <sect2 id="zend.service.twitter.friendship">
620 <title>Friendship Methods</title>
625 <methodname>create()</methodname> befriends the user specified in the
626 <code>id</code> parameter with the authenticating user.
629 <example id="zend.service.twitter.friendship.create">
630 <title>Creating friend</title>
632 <programlisting language="php"><![CDATA[
633 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
634 $response = $twitter->friendship->create('mynewfriend');
641 <methodname>destroy()</methodname> discontinues friendship with the user
642 specified in the <code>id</code> parameter and the authenticating user.
645 <example id="zend.service.twitter.friendship.destroy">
646 <title>Deleting friend</title>
648 <programlisting language="php"><![CDATA[
649 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
650 $response = $twitter->friendship->destroy('myoldfriend');
657 <methodname>exists()</methodname> tests if a friendship exists between the
658 user specified in the <code>id</code> parameter and the authenticating user.
661 <example id="zend.service.twitter.friendship.exists">
662 <title>Checking friend existence</title>
664 <programlisting language="php"><![CDATA[
665 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
666 $response = $twitter->friendship->exists('myfriend');
673 <sect2 id="zend.service.twitter.favorite">
674 <title>Favorite Methods</title>
679 <methodname>favorites()</methodname> returns the 20 most recent favorite
680 statuses for the authenticating user or user specified by the
681 <code>id</code> parameter.
684 <example id="zend.service.twitter.favorite.favorites">
685 <title>Retrieving favorites</title>
687 <programlisting language="php"><![CDATA[
688 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
689 $response = $twitter->favorite->favorites();
694 The <methodname>favorites()</methodname> method accepts an array of optional
695 parameters to modify the query.
701 <code>id</code> specifies the ID or screen name of the user for whom to
702 request a list of favorite statuses.
708 <code>page</code> specifies which page you want to return.
716 <methodname>create()</methodname> favorites the status specified in the
717 <code>id</code> parameter as the authenticating user.
720 <example id="zend.service.twitter.favorite.create">
721 <title>Creating favorites</title>
723 <programlisting language="php"><![CDATA[
724 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
725 $response = $twitter->favorite->create(12351);
732 <methodname>destroy()</methodname> un-favorites the status specified in the
733 <code>id</code> parameter as the authenticating user.
736 <example id="zend.service.twitter.favorite.destroy">
737 <title>Deleting favorites</title>
739 <programlisting language="php"><![CDATA[
740 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
741 $response = $twitter->favorite->destroy(12351);
748 <sect2 id="zend.service.twitter.block">
749 <title>Block Methods</title>
754 <methodname>exists()</methodname> checks if the authenticating user is blocking
755 a target user and can optionally return the blocked user's object if a
759 <example id="zend.service.twitter.block.exists">
760 <title>Checking if block exists</title>
762 <programlisting language="php"><![CDATA[
763 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
765 // returns true or false
766 $response = $twitter->block->exists('blockeduser');
768 // returns the blocked user's info if the user is blocked
769 $response2 = $twitter->block->exists('blockeduser', true);
774 The <methodname>favorites()</methodname> method accepts a second
781 <code>returnResult</code> specifies whether or not return the user
782 object instead of just <constant>TRUE</constant> or
783 <constant>FALSE</constant>.
791 <methodname>create()</methodname> blocks the user specified in the
792 <code>id</code> parameter as the authenticating user and destroys a friendship
793 to the blocked user if one exists. Returns the blocked user in the requested
794 format when successful.
797 <example id="zend.service.twitter.block.create">
798 <title>Blocking a user</title>
800 <programlisting language="php"><![CDATA[
801 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
802 $response = $twitter->block->create('usertoblock);
809 <methodname>destroy()</methodname> un-blocks the user specified in the
810 <code>id</code> parameter for the authenticating user. Returns the un-blocked
811 user in the requested format when successful.
814 <example id="zend.service.twitter.block.destroy">
815 <title>Removing a block</title>
817 <programlisting language="php"><![CDATA[
818 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
819 $response = $twitter->block->destroy('blockeduser');
826 <methodname>blocking()</methodname> returns an array of user objects that the
827 authenticating user is blocking.
830 <example id="zend.service.twitter.block.blocking">
831 <title>Who are you blocking</title>
833 <programlisting language="php"><![CDATA[
834 $twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
836 // return the full user list from the first page
837 $response = $twitter->block->blocking();
839 // return an array of numeric user IDs from the second page
840 $response2 = $twitter->block->blocking(2, true);
845 The <methodname>favorites()</methodname> method accepts two optional parameters.
851 <code>page</code> specifies which page ou want to return. A single page
858 <code>returnUserIds</code> specifies whether to return an array of
859 numeric user IDs the authenticating user is blocking instead of an
860 array of user objects.
868 <xi:include href="Zend_Service_Twitter_Search.xml" />