Zend_Service_Twitter
provides a client for the Twitter REST API.
Zend_Service_Twitter
will allow you to query the public timeline and if you provide a username
and password for Twitter it will allow you to get and update your status, reply to friends,
direct message friends, mark tweets as favorite and much more.
Zend_Service_Twitter
is implementing a REST service and all methods return an instance of
Zend_Rest_Client_Result
.
Zend_Service_Twitter
is broken up into subsections so you can easily identify which type of call
is being requested.
account
, make sure that your account credentials are valid, check your api rate limit
and end the current session for the authenticated user.
status
, retrieves the public and user timelines and allows you to show, update, destroy
and retrieve replies for the authenticated user.
user
, retrieves the friends, followers for the authenticated user. With the
show method you can return extended information about the passed in user.
directMessage
, retrieves the authenticated users received direct message and allows you to send and
delete new direct message.
friendship
, create or remove a friendship for the authenticated user.
favorite
, list, create or remove a favorite tweet.
With the exception of fetching the public timeline Zend_Service_Twitter
requires
authentication to work. Twitter currently uses HTTP Basic Authentication.
You can pass in your username or registered email along with your password for twitter to login.
例 47.102. Creating the Twitter Class
The following code sample is how you create the Twitter Service and pass in your username and password and then verify that they are correct.
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); // verify your credentials with twitter $response = $twitter->account->verifyCredentials();
verifyCredentials
, Use this method to test if supplied user credentials are valid with minimal overhead.
例 47.103. Verifying credentials
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->account->verifyCredentials();
endSession
, Use this method to sign users out of client-facing applications.
例 47.104. Sessions ending
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->account->endSession();
rateLimitStatus
, Returns the remaining number of API requests available to the authenticating user
before the API limit is reached for the current hour.
例 47.105. Rating limit status
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->account->rateLimitStatus();
publicTimeline
, Returns the 20 most recent statuses from non-protected users with a custom user icon.
The public timeline is cached by twitter for 60 seconds.
例 47.106. Retrieving public timeline
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->publicTimeline();
friendsTimeline
, Returns the 20 most recent statuses posted by the authenticating user and that
user's friends.
例 47.107. Retrieving friends timeline
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->friendsTimeline();
The friendsTimeline
method accepts an array of optional parameters to modify the query.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
userTimeline
, Returns the 20 most recent statuses posted from the authenticating user.
例 47.108. Retrieving user timeline
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->userTimeline();
The userTimeline
method accepts an array of optional parameters to modify the query.
id
, Specifies the ID or screen name of the user for whom to return the friends_timeline.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
count
, Specifies the number of statuses to retrieve. May not be greater than 200.
show
, Returns a single status, specified by the id parameter below. The status's author will be returned inline.
This method required a tweet id to be passed in.
例 47.109. Showing user status
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->show(1234);
update
, Updates the authenticating user's status. This method requires that you pass in the status update that
you want to post to twitter. A second optional parameter is the id of the tweet that you are replying to.
例 47.110. Updating user status
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->update('My Great Tweet');
The update
method accepts a second additional parameter.
in_reply_to_status_id
, The ID of an existing status that the status to be posted is in reply to.
replies
, Returns the 20 most recent @replies (status updates prefixed with @username) for the
authenticating user.
例 47.111. Showing user replies
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->replies();
The replies
method accepts an array of optional parameters to modify the query.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
since_id
, Returns only statuses with an ID greater than (that is, more recent than) the specified ID.
destroy
, Destroys the status specified by the required ID parameter.
例 47.112. Deleting user status
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->status->destroy(12345);
friends
, Returns up to 100 of the authenticating user's friends who have
most recently updated, each with current status inline.
例 47.113. Retrieving user friends
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->user->friends();
The friends
method accepts an array of optional parameters to modify the query.
id
, Specifies the ID or screen name of the user for whom to return a list of friends.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
followers
, Returns the authenticating user's followers, each with current status inline.
例 47.114. Retrieving user followers
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->user->followers();
The followers
method accepts an array of optional parameters to modify the query.
id
, Specifies the ID or screen name of the user for whom to return a list of followers.
page
, Which page you want to return.
show
, Returns extended information of a given user, specified by ID or screen name as per the
required id parameter below
例 47.115. Showing user informations
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->user->show('myfriend');
messages
, Returns a list of the 20 most recent direct messages sent to the authenticating user.
例 47.116. Retrieving recent direct messages received
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->directMessage->messages();
The message
method accepts an array of optional parameters to modify the query.
since_id
, Returns only direct messages with an ID greater than (that is, more recent than)
the specified ID.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
sent
, Returns a list of the 20 most recent direct messages sent by the authenticating user.
例 47.117. Retrieving recent direct messages sent
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->directMessage->sent();
The sent
method accepts an array of optional parameters to modify the query.
since_id
, Returns only direct messages with an ID greater than (that is, more recent than)
the specified ID.
since
, Narrows the returned results to just those statuses created after the specified
date/time (up to 24 hours old).
page
, Which page you want to return.
new
, Sends a new direct message to the specified user from the authenticating user.
Requires both the user and text parameters below.
例 47.118. Sending direct message
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->directMessage->new('myfriend', 'mymessage');
destroy
, Destroys the direct message specified in the required ID parameter. The authenticating user
must be the recipient of the specified direct message.
例 47.119. Deleting direct message
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->directMessage->destroy(123548);
create
, Befriends the user specified in the ID parameter as the authenticating user.
例 47.120. Creating friend
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->friendship->create('mynewfriend');
destroy
, Discontinues friendship with the user specified in the ID parameter as the authenticating user.
例 47.121. Deleting friend
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->friendship->destroy('myoldfriend');
exists
, Tests if a friendship exists between the authenticated user and the passed in user.
例 47.122. Checking friend existence
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->friendship->exists('myfriend');
favorites
, Returns the 20 most recent favorite statuses for the authenticating user or user specified by the ID parameter
例 47.123. Retrieving favorites
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->favorite->favorites();
id
, The ID or screen name of the user for whom to request a list of favorite statuses.
page
, Which page you want to return.
create
, Favorites the status specified in the ID parameter as the authenticating user..
例 47.124. Creating favorites
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->favorite->create(12351);
destroy
, Un-favorites the status specified in the ID parameter as the authenticating user.
例 47.125. Deleting favorites
$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword'); $response = $twitter->favorite->destroy(12351);
Zend_Service_Twitter_Search
provides a client for the Twitter Search API.
The Twitter Search service is use to search Twitter. Currently it only
returns data in Atom or JSON format but a full REST service is in the future
which will support XML responses.
Returns the top ten queries that are currently trending on Twitter. The response includes the time of the request, the name of each trending topic, and the url to the Twitter Search results page for that topic. Currently the search API for trends only supports a JSON return so the function returns an array.
$twitter_search = new Zend_Service_Twitter_Search(); $twitter_trends = $twitter_search->trends(); foreach($twitter_trends as $trend) { print $trend['name'] . ' - ' . $trend['url'] . PHP_EOL }
The return array has two values in it:
name
, the name of trend.
url
, the url to see the tweets
for that trend.
Using the search method returns tweets that match a specific query. There are a number of Search Operators that you can use to query with.
The search method can accept six different optional URL parameters passed in as an array:
lang
, restricts the tweets to a given language, lang must
be given by an ISO 639-1 code.
rpp
, the number of tweets to return per page,
up to a max of 100.
page
, the page number to return, up to a max of
roughly 1500 results (based on rpp * page)
since_id
, returns tweets with status ids
greater than the given id.
show_user
, when "true", adds ">user<:"
to the beginning of the tweet. This is useful for readers
that do not display Atom's author field. The default is "false"
geocode
, returns tweets by users located within a given radius of the given
latitude/longitude, where the user's location is taken from their Twitter profile. The
parameter value is specified by "latitude,longitude,radius", where radius units must
be specified as either "mi" (miles) or "km" (kilometers).
例 47.126. JSON Search Example
The following code sample will return an array with the values search results
$twitter_search = new Zend_Service_Twitter_Search('json'); $search_results = $twitter_search->search('zend', array('lang' => 'en'));
例 47.127. ATOM Search Example
The following code sample will return a Zend_Feed_Atom
object.
$twitter_search = new Zend_Service_Twitter_Search('atom'); $search_results = $twitter_search->search('zend', array('lang' => 'en'));
While the Twitter Search API only specifies two methods,
Zend_Service_Twitter_Search
has additional accessors
that may be used for modifying internal properties.
getResponseType()
and setResponseType()
allow you to retrieve and modify the response type of the search
between JSON and ATOM.