Zend Framework  1.12
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Zend_Service_Twitter Class Reference

Public Member Functions

 __construct ($options=null, Zend_Oauth_Consumer $consumer=null, Zend_Http_Client $httpClient=null)
 Constructor.
 
 __get ($type)
 Proxy service methods.
 
 __call ($method, $params)
 Method overloading.
 
 setHttpClient (Zend_Http_Client $client)
 Set HTTP client.
 
 getHttpClient ()
 Get the HTTP client.
 
 getUsername ()
 Retrieve username.
 
 setUsername ($value)
 Set username.
 
 isAuthorised ()
 Checks for an authorised state.
 
 accountVerifyCredentials ()
 Verify Account Credentials.
 
 applicationRateLimitStatus ()
 Returns the number of api requests you have left per hour.
 
 blocksCreate ($id)
 Blocks the user specified in the ID parameter as the authenticating user.
 
 blocksDestroy ($id)
 Un-blocks the user specified in the ID parameter for the authenticating user.
 
 blocksIds ($cursor=-1)
 Returns an array of user ids that the authenticating user is blocking.
 
 blocksList ($cursor=-1)
 Returns an array of user objects that the authenticating user is blocking.
 
 directMessagesDestroy ($id)
 Destroy a direct message.
 
 directMessagesMessages (array $options=array())
 Retrieve direct messages for the current user.
 
 directMessagesNew ($user, $text)
 Send a direct message to a user.
 
 directMessagesSent (array $options=array())
 Retrieve list of direct messages sent by current user.
 
 favoritesCreate ($id)
 Mark a status as a favorite.
 
 favoritesDestroy ($id)
 Remove a favorite.
 
 favoritesList (array $options=array())
 Fetch favorites.
 
 friendshipsCreate ($id, array $params=array())
 Create friendship.
 
 friendshipsDestroy ($id)
 Destroy friendship.
 
 searchTweets ($query, array $options=array())
 Search tweets.
 
 statusesDestroy ($id)
 Destroy a status message.
 
 statusesHomeTimeline (array $options=array())
 Friend Timeline Status.
 
 statusesMentionsTimeline (array $options=array())
 Get status replies.
 
 statusesSample ()
 Public Timeline status.
 
 statusesShow ($id)
 Show a single status.
 
 statusesUpdate ($status, $inReplyToStatusId=null)
 Update user's current status.
 
 statusesUserTimeline (array $options=array())
 User Timeline status.
 
 usersSearch ($query, array $options=array())
 Search users.
 
 usersShow ($id)
 Show extended information on a user.
 

Public Attributes

const API_BASE_URI = 'https://api.twitter.com/1.1/'
 Base URI for all API calls.
 
const OAUTH_BASE_URI = 'https://api.twitter.com/oauth'
 OAuth Endpoint.
 
const STATUS_MAX_CHARACTERS = 246
 246 is the current limit for a status message, 140 characters are displayed initially, with the remainder linked from the web UI or client.
 

Protected Member Functions

 init ()
 Initialize HTTP authentication.
 
 validInteger ($int)
 Protected function to validate that the integer is valid or return a 0.
 
 validateScreenName ($name)
 Validate a screen name using Twitter rules.
 
 prepare ($path, Zend_Http_Client $client)
 Call a remote REST web service URI.
 
 get ($path, array $query=array())
 Performs an HTTP GET request to the $path.
 
 post ($path, $data=null)
 Performs an HTTP POST request to $path.
 
 performPost ($method, $data, Zend_Http_Client $client)
 Perform a POST or PUT.
 
 createUserParameter ($id, array $params)
 Create a parameter representing the user.
 

Protected Attributes

 $cookieJar
 
 $dateFormat = 'D, d M Y H:i:s T'
 
 $httpClient = null
 
 $methodType
 
 $oauthConsumer = null
 
 $methodTypes
 
 $options = array()
 
 $username
 

Constructor & Destructor Documentation

__construct (   $options = null,
Zend_Oauth_Consumer  $consumer = null,
Zend_Http_Client  $httpClient = null 
)

Constructor.

Parameters
null | array | Zend_Config$options
null | Zend_Oauth_Consumer$consumer
null | Zend_Http_Client$httpClient

Member Function Documentation

__call (   $method,
  $params 
)

Method overloading.

Parameters
string$method
array$params
Returns
mixed
Exceptions
Exception\BadMethodCallExceptionif unable to find method
__get (   $type)

Proxy service methods.

Parameters
string$type
Returns
Twitter
Exceptions
Exception\DomainExceptionIf method not in method types list
accountVerifyCredentials ( )

Verify Account Credentials.

Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
applicationRateLimitStatus ( )

Returns the number of api requests you have left per hour.

Todo:
Have a separate payload object to represent rate limits
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
blocksCreate (   $id)

Blocks the user specified in the ID parameter as the authenticating user.

Destroys a friendship to the blocked user if it exists.

Parameters
integer | string$idThe ID or screen name of a user to block.
Exceptions
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
blocksDestroy (   $id)

Un-blocks the user specified in the ID parameter for the authenticating user.

Parameters
integer | string$idThe ID or screen_name of the user to un-block.
Exceptions
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
blocksIds (   $cursor = -1)

Returns an array of user ids that the authenticating user is blocking.

Parameters
integer$cursorOptional. Specifies the cursor position at which to begin listing ids; defaults to first "page" of results.
Exceptions
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
blocksList (   $cursor = -1)

Returns an array of user objects that the authenticating user is blocking.

Parameters
integer$cursorOptional. Specifies the cursor position at which to begin listing ids; defaults to first "page" of results.
Exceptions
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
createUserParameter (   $id,
array  $params 
)
protected

Create a parameter representing the user.

Determines if $id is an integer, and, if so, sets the "user_id" parameter. If not, assumes the $id is the "screen_name".

Parameters
int | string$id
array$params
Returns
array
directMessagesDestroy (   $id)

Destroy a direct message.

Parameters
int$idID of message to destroy
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
directMessagesMessages ( array  $options = array())

Retrieve direct messages for the current user.

$options may include one or more of the following keys

  • count: return page X of results
  • since_id: return statuses only greater than the one specified
  • max_id: return statuses with an ID less than (older than) or equal to that specified
  • include_entities: setting to false will disable embedded entities
  • skip_status:setting to true, "t", or 1 will omit the status in returned users
Parameters
array$options
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
directMessagesNew (   $user,
  $text 
)

Send a direct message to a user.

Parameters
int | string$userUser to whom to send message
string$textMessage to send to user
Exceptions
Exception\InvalidArgumentExceptionif message is empty
Exception\OutOfRangeExceptionif message is too long
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
directMessagesSent ( array  $options = array())

Retrieve list of direct messages sent by current user.

$options may include one or more of the following keys

  • count: return page X of results
  • page: return starting at page
  • since_id: return statuses only greater than the one specified
  • max_id: return statuses with an ID less than (older than) or equal to that specified
  • include_entities: setting to false will disable embedded entities
Parameters
array$options
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
favoritesCreate (   $id)

Mark a status as a favorite.

Parameters
int$idStatus ID you want to mark as a favorite
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
favoritesDestroy (   $id)

Remove a favorite.

Parameters
int$idStatus ID you want to de-list as a favorite
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
favoritesList ( array  $options = array())

Fetch favorites.

$options may contain one or more of the following:

  • user_id: Id of a user for whom to fetch favorites
  • screen_name: Screen name of a user for whom to fetch favorites
  • count: number of tweets to attempt to retrieve, up to 200
  • since_id: return results only after the specified tweet id
  • max_id: return results with an ID less than (older than) or equal to the specified ID
  • include_entities: when set to false, entities member will be omitted
Parameters
array$params
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
friendshipsCreate (   $id,
array  $params = array() 
)

Create friendship.

Parameters
int | string$idUser ID or name of new friend
array$paramsAdditional parameters to pass
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
friendshipsDestroy (   $id)

Destroy friendship.

Parameters
int | string$idUser ID or name of friend to remove
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
get (   $path,
array  $query = array() 
)
protected

Performs an HTTP GET request to the $path.

Parameters
string$path
array$queryArray of GET parameters
Exceptions
Zend_Http_Client_Exception
Returns
Zend_Http_Response
getHttpClient ( )

Get the HTTP client.

Lazy loads one if none present

Returns
Zend_Http_Client
getUsername ( )

Retrieve username.

Returns
string
init ( )
protected

Initialize HTTP authentication.

Returns
void
Exceptions
Exception\DomainExceptionif unauthorised
isAuthorised ( )

Checks for an authorised state.

Returns
bool
performPost (   $method,
  $data,
Zend_Http_Client  $client 
)
protected

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.

Parameters
mixed$method
mixed$data
Returns
Zend_Http_Response
post (   $path,
  $data = null 
)
protected

Performs an HTTP POST request to $path.

Parameters
string$path
mixed$dataRaw data to send
Exceptions
Zend_Http_Client_Exception
Returns
Zend_Http_Response
prepare (   $path,
Zend_Http_Client  $client 
)
protected

Call a remote REST web service URI.

Parameters
string$pathThe path to append to the URI
Zend_Http_Client$client
Exceptions
Zend_Http_Client_Exception
Returns
void

Do this each time to ensure oauth calls do not inject new params

searchTweets (   $query,
array  $options = array() 
)

Search tweets.

$options may include any of the following:

  • geocode: a string of the form "latitude, longitude, radius"
  • lang: restrict tweets to the two-letter language code
  • locale: query is in the given two-letter language code
  • result_type: what type of results to receive: mixed, recent, or popular
  • count: number of tweets to return per page; up to 100
  • until: return tweets generated before the given date
  • since_id: return resutls with an ID greater than (more recent than) the given ID
  • max_id: return results with an ID less than (older than) the given ID
  • include_entities: whether or not to include embedded entities
Parameters
string$query
array$options
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
setHttpClient ( Zend_Http_Client  $client)

Set HTTP client.

Parameters
Zend_Http_Client$client
Returns
self
setUsername (   $value)

Set username.

Parameters
string$value
Returns
self
statusesDestroy (   $id)

Destroy a status message.

Parameters
int$idID of status to destroy
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesHomeTimeline ( array  $options = array())

Friend Timeline Status.

$options may include one or more of the following keys

  • count: number of tweets to attempt to retrieve, up to 200
  • since_id: return results only after the specified tweet id
  • max_id: return results with an ID less than (older than) or equal to the specified ID
  • trim_user: when set to true, "t", or 1, user object in tweets will include only author's ID.
  • contributor_details: when set to true, includes screen_name of each contributor
  • include_entities: when set to false, entities member will be omitted
  • exclude_replies: when set to true, will strip replies appearing in the timeline
Parameters
array$params
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesMentionsTimeline ( array  $options = array())

Get status replies.

$options may include one or more of the following keys

  • count: number of tweets to attempt to retrieve, up to 200
  • since_id: return results only after the specified tweet id
  • max_id: return results with an ID less than (older than) or equal to the specified ID
  • trim_user: when set to true, "t", or 1, user object in tweets will include only author's ID.
  • contributor_details: when set to true, includes screen_name of each contributor
  • include_entities: when set to false, entities member will be omitted
Parameters
array$options
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesSample ( )

Public Timeline status.

Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesShow (   $id)

Show a single status.

Parameters
int$idId of status to show
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesUpdate (   $status,
  $inReplyToStatusId = null 
)

Update user's current status.

Todo:
Support additional parameters supported by statuses/update endpoint
Parameters
string$status
null | int$inReplyToStatusId
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\OutOfRangeExceptionif message is too long
Exception\InvalidArgumentExceptionif message is empty
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
statusesUserTimeline ( array  $options = array())

User Timeline status.

$options may include one or more of the following keys

  • user_id: Id of a user for whom to fetch favorites
  • screen_name: Screen name of a user for whom to fetch favorites
  • count: number of tweets to attempt to retrieve, up to 200
  • since_id: return results only after the specified tweet id
  • max_id: return results with an ID less than (older than) or equal to the specified ID
  • trim_user: when set to true, "t", or 1, user object in tweets will include only author's ID.
  • exclude_replies: when set to true, will strip replies appearing in the timeline
  • contributor_details: when set to true, includes screen_name of each contributor
  • include_rts: when set to false, will strip native retweets
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
usersSearch (   $query,
array  $options = array() 
)

Search users.

$options may include any of the following:

  • page: the page of results to retrieve
  • count: the number of users to retrieve per page; max is 20
  • include_entities: if set to boolean true, include embedded entities
Parameters
string$query
array$options
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
usersShow (   $id)

Show extended information on a user.

Parameters
int | string$idUser ID or name
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Exception\DomainExceptionif unable to decode JSON payload
Returns
Zend_Service_Twitter_Response
validateScreenName (   $name)
protected

Validate a screen name using Twitter rules.

Parameters
string$name
Returns
string
Exceptions
Exception\InvalidArgumentException
validInteger (   $int)
protected

Protected function to validate that the integer is valid or return a 0.

Parameters
$int
Exceptions
Zend_Http_Client_Exceptionif HTTP request fails or times out
Returns
integer

Member Data Documentation

$cookieJar
protected
$dateFormat = 'D, d M Y H:i:s T'
protected
$httpClient = null
protected
$methodType
protected
$methodTypes
protected
Initial value:
= array(
'account',
'application',
'blocks',
'directmessages',
'favorites',
'friendships',
'search',
'statuses',
'users',
)
$oauthConsumer = null
protected
$options = array()
protected
$username
protected
const API_BASE_URI = 'https://api.twitter.com/1.1/'

Base URI for all API calls.

const OAUTH_BASE_URI = 'https://api.twitter.com/oauth'

OAuth Endpoint.

const STATUS_MAX_CHARACTERS = 246

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...