ZendService\Twitter provides a client for the Twitter API. ZendService\Twitter allows you to query the public timeline. If you provide a username and OAuth details for Twitter, or your access token and secret, it will allow you to get and update your status, reply to friends, direct message friends, mark tweets as favorites, and much more.
ZendService\Twitter wraps all web service operations, including OAuth, and all methods return an instance of ZendService\Twitter\Response.
ZendService\Twitter is broken up into subsections so you can easily identify which type of call is being requested.
With the exception of fetching the public timeline, ZendService\Twitter requires authentication as a valid user. This is achieved using the OAuth authentication protocol. OAuth is the only supported authentication mode for Twitter as of August 2010. The OAuth implementation used by ZendService\Twitter is ZendOAuth.
Creating the Twitter Class
ZendService\Twitter must authorize itself, on behalf of a user, before use with the Twitter API (except for public timeline). This must be accomplished using OAuth since Twitter has disabled it’s basic HTTP authentication as of August 2010.
There are two options to establishing authorization. The first is to implement the workflow of ZendOAuth via ZendService\Twitter which proxies to an internal ZendOAuth\Consumer object. Please refer to the ZendOAuth documentation for a full example of this workflow - you can call all documented ZendOAuth\Consumer methods on ZendService\Twitter including constructor options. You may also use ZendOAuth directly and only pass the resulting access token into ZendService\Twitter. This is the normal workflow once you have established a reusable access token for a particular Twitter user. The resulting OAuth access token should be stored to a database for future use (otherwise you will need to authorize for every new instance of ZendService\Twitter). Bear in mind that authorization via OAuth results in your user being redirected to Twitter to give their consent to the requested authorization (this is not repeated for stored access tokens). This will require additional work (i.e. redirecting users and hosting a callback URL) over the previous HTTP authentication mechanism where a user just needed to allow applications to store their username and password.
The following example demonstrates setting up ZendService\Twitter which is given an already established OAuth access token. Please refer to the ZendOAuth documentation to understand the workflow involved. The access token is a serializable object, so you may store the serialized object to a database, and unserialize it at retrieval time before passing the objects into ZendService\Twitter. The ZendOAuth documentation demonstrates the workflow and objects involved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /**
* We assume $serializedToken is the serialized token retrieved from a database
* or even $_SESSION (if following the simple ZendOAuth documented example)
*/
$token = unserialize($serializedToken);
$twitter = new ZendService\Twitter\Twitter(array(
'accessToken' => $token,
'oauth_options' => array(
'username' => 'johndoe',
),
));
// verify user's credentials with Twitter
$response = $twitter->account->verifyCredentials();
|
Note
In order to authenticate with Twitter, ALL applications MUST be registered with Twitter in order to receive a Consumer Key and Consumer Secret to be used when authenticating with OAuth. This can not be reused across multiple applications - you must register each new application separately. Twitter access tokens have no expiry date, so storing them to a database is advised (they can, of course, be refreshed simply be repeating the OAuth authorization process). This can only be done while interacting with the user associated with that access token.
The previous pre-OAuth version of ZendService\Twitter allowed passing in a username as the first parameter rather than within an array. This is no longer supported.
If you have registered an application with Twitter, you can also use the access token and access token secret they provide you in order to setup the OAuth consumer. This can be done as follows:
1 2 3 4 5 6 7 8 9 10 | $twitter = new Zend_Service_Twitter(array(
'access_token' => array( // or use "accessToken" as the key; both work
'token' => 'your-access-token',
'secret' => 'your-access-token-secret',
),
'oauth_options' => array( // or use "oauthOptions" as the key; both work
'consumerKey' => 'your-consumer-key',
'consumerSecret' => 'your-consumer-secret',
),
));
|
If desired, you can also specify a specific HTTP client instance to use, or provide configuration for the HTTP client. To provide the HTTP client, use the http_client or httpClient key, and provide an instance. To provide HTTP client configuration for setting up an instance, use the key http_client_options or httpClientOptions. As a full example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $twitter = new Zend_Service_Twitter(array(
'access_token' => array( // or use "accessToken" as the key; both work
'token' => 'your-access-token',
'secret' => 'your-access-token-secret',
),
'oauth_options' => array( // or use "oauthOptions" as the key; both work
'consumerKey' => 'your-consumer-key',
'consumerSecret' => 'your-consumer-secret',
),
'http_client_options' => array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
),
));
|
verifyCredentials() tests if supplied user credentials are valid with minimal overhead.
Verifying credentials
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->account->verifyCredentials();
|
rateLimitStatus() returns the remaining number of API requests available to the authenticating user before the API limit is reached for the current hour.
Rating limit status
1 2 3 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->application->rateLimitStatus();
$userTimelineLimit = $response->resources->statuses->{'/statuses/user_timeline'}->remaining;
|
create() blocks the user specified in the id parameter as the authenticating user and destroys a friendship to the blocked user if one exists. Returns the blocked user in the requested format when successful.
Blocking a user
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->blocks->create('usertoblock');
|
destroy() un-blocks the user specified in the id parameter for the authenticating user. Returns the un-blocked user in the requested format when successful.
Removing a block
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->blocks->destroy('blockeduser');
|
ids() returns an array of user identifiers that the authenticating user is blocking.
Who are you blocking (identifiers only)
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->blocks->ids();
|
list() returns an array of user objects that the authenticating user is blocking.
Who are you blocking
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->blocks->list();
|
messages() returns a list of the 20 most recent direct messages sent to the authenticating user.
Retrieving recent direct messages received
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->directMessages->messages();
|
The message() method accepts an array of optional parameters to modify the query.
sent() returns a list of the 20 most recent direct messages sent by the authenticating user.
Retrieving recent direct messages sent
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->directMessages->sent();
|
The sent() method accepts an array of optional parameters to modify the query.
new() sends a new direct message to the specified user from the authenticating user. Requires both the user and text parameters below.
Sending a direct message
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->directMessages->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.
Deleting a direct message
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->directMessages->destroy(123548);
|
list() returns the 20 most recent favorite statuses for the authenticating user or user specified by the id parameter.
Retrieving favorites
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->favorites->list();
|
The list() method accepts an array of optional parameters to modify the query.
create() favorites the status specified in the id parameter as the authenticating user.
Creating favorites
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->favorites->create(12351);
|
destroy() un-favorites the status specified in the id parameter as the authenticating user.
Deleting a favorite
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->favorites->destroy(12351);
|
create() befriends the user specified in the id parameter with the authenticating user.
Creating a friend
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->friendships->create('mynewfriend');
|
destroy() discontinues friendship with the user specified in the id parameter and the authenticating user.
Deleting a friend
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->friendships->destroy('myoldfriend');
|
tweets() returns a list of tweets matching the criteria specified in $query. By default, 15 will be returned, but this value may be changed using the count option.
Searching for tweets
1 2 | $twitter = new Zend_Service_Twitter($options);
$response = $twitter->search->tweets('#zendframework');
|
The tweets() method accepts an optional second argument, array of optional parameters to modify the query.
sample() 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.
Retrieving the public timeline
1 2 | $twitter = new ZendService\Twitter($options);
$response = $twitter->statuses->sample();
|
homeTimeline() returns the 20 most recent statuses posted by the authenticating user and that user’s friends.
Retrieving the home timeline
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->homeTimeline();
|
The homeTimeline() method accepts an array of optional parameters to modify the query.
userTimeline() returns the 20 most recent statuses posted from the authenticating user.
Retrieving the user timeline
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->userTimeline();
|
The userTimeline() method accepts an array of optional parameters to modify the query.
show() returns a single status, specified by the id parameter below. The status’ author will be returned inline.
Showing user status
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->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.
Updating user status
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->update('My Great Tweet');
|
The update() method accepts a second additional parameter.
mentionsTimeline() returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.
Showing user replies
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->mentionsTimeline();
|
The mentionsTimeline() method accepts an array of optional parameters to modify the query.
destroy() destroys the status specified by the required id parameter.
Deleting user status
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->statuses->destroy(12345);
|
show() returns extended information of a given user, specified by ID or screen name as per the required id parameter below.
Showing user information
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->users->show('myfriend');
|
search() will search for users matching the query provided.
Searching for users
1 2 | $twitter = new ZendService\Twitter\Twitter($options);
$response = $twitter->users->search('Zend');
|
The search() method accepts an array of optional parameters to modify the query.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.