Zend_Service_Twitter

Introduction

Zend_Service_Twitter fournit un client pour l'API REST de Twitter. Zend_Service_Twitter vous permet d'interroger les fils (timeline) publics. En fournissant un nom d'utilisateur et un mot de passe pour Twitter, il vous permettra également de récupérer et mettre à jour votre statut, de répondre à des amis, de leur envoyer des messages directs, de marquer des tweets comme favoris et beaucoup d'autres choses.

Zend_Service_Twitter implémente un service REST, et toutes ses méthodes retournes une instance de Zend_Rest_Client_Result.

Zend_Service_Twitter et subdivisé en sections, ainsi vous pouvez facilement identifier le type d'appel qui est demandé.

  • account s'assure que vos données de compte sont valides, vérifie votre taux limite pour l'API et termine la session courante pour l'utilisateur authentifié.

  • status retourne les fils publics et ceux de l'utilisateur et montre, met à jour, détruit et retourne des réponses pour l'utilisateur authentifié.

  • user récupère les amis et 'followers' de l'utilisateur authentifié et retourne de plus amples informations sur l'utilisateur passé en paramètre.

  • directMessage récupère les messages directs reçus par l'utilisateur authentifié, supprime les messages directs et permet également d'envoyer des messages directs.

  • friendship crée et supprime des amitiés pour l'utilisateur authentifié.

  • favorite liste, crée et détruit des tweets favoris.

  • block bloque et débloque des utilisateurs qui vous suivent.

Authentification

A l'exception de la récupération du fil public, Zend_Service_Twitter nécessite une authentification pour fonctionner. Twitter utilise l'Authentification HTTP basique. Vous pouvez lui passer votre nom d'utilisateur ou votre email utilisé pour l'enregistrement de votre compte ainsi que votre mot de passe pour vous connecter à Twitter.

Exemple 804. Créer la classe Twitter

L'exemple de code suivant décrit comment créer le service Twitter, lui passer vos nom d'utilisateur et mot de passe et vérifier qu'ils sont corrects.

$twitter = new Zend_Service_Twitter('myusername', 'mysecretpassword');
// vérifie vos données de connexion avec Twitter
$response = $twitter->account->verifyCredentials();

Vous pouvez également passer un tableau qui contient le nom d'utilisateur et le mot de passe en tant que premier argument

$userInfo   = array('username' => 'foo', 'password' => 'bar');
$twitter    = new Zend_Service_Twitter($userInfo);
// vérifie vos données de connexion avec Twitter
$response = $twitter->account->verifyCredentials();

Account Methods

  • verifyCredentials() tests if supplied user credentials are valid with minimal overhead.

    Exemple 805. Verifying credentials

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->account->verifyCredentials();
    

Application Methods

  • rateLimitStatus() returns the remaining number of API requests available to the authenticating user before the API limit is reached for the current hour.

    Exemple 806. Rating limit status

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->application->rateLimitStatus();
    $userTimelineLimit = $response->resources->statuses->{'/statuses/user_timeline'}->remaining;
    

Block Methods

  • 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 when successful.

    Exemple 807. Blocking a user

    $twitter  = new Zend_Service_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.

    Exemple 808. Removing a block

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->blocks->destroy('blockeduser');
    

  • ids() returns an array of user identifiers that the authenticating user is blocking.

    Exemple 809. Who are you blocking (identifiers only)

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->blocks->ids();
    

  • list() returns an array of user objects that the authenticating user is blocking.

    Exemple 810. Who are you blocking

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->blocks->list();
    

Direct Message Methods

  • messages() returns a list of the 20 most recent direct messages sent to the authenticating user.

    Exemple 811. Retrieving recent direct messages received

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->directMessages->messages();
    

    The messages() method accepts an array of optional parameters to modify the query.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • skip_status, when set to boolean true, "t", or 1 will skip including a user's most recent status in the results.

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

  • sent() returns a list of the 20 most recent direct messages sent by the authenticating user.

    Exemple 812. Retrieving recent direct messages sent

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->directMessages->sent();
    

    The sent() method accepts an array of optional parameters to modify the query.

    • count specifies the number of statuses to return, up to 20.

    • page specifies the page of results to return, based on the count provided.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

  • new() sends a new direct message to the specified user from the authenticating user. Requires both the user and text parameters below.

    Exemple 813. Sending a direct message

    $twitter  = new Zend_Service_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.

    Exemple 814. Deleting a direct message

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->directMessages->destroy(123548);
    

Favorites Methods

  • list() returns the 20 most recent favorite statuses for the authenticating user or user specified by the user_id or screen_name parameter.

    Exemple 815. Retrieving favorites

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->favorites->list();
    

    The list() method accepts an array of optional parameters to modify the query.

    • user_id specifies the ID of the user for whom to return the timeline.

    • screen_name specifies the screen name of the user for whom to return the timeline.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

  • create() favorites the status specified in the id parameter as the authenticating user.

    Exemple 816. Creating a favorite

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->favorites->create(12351);
    

  • destroy() un-favorites the status specified in the id parameter as the authenticating user.

    Exemple 817. Deleting favorites

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->favorites->destroy(12351);
    

Friendship Methods

  • create() befriends the user specified in the id parameter with the authenticating user.

    Exemple 818. Creating a friend

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->friendships->create('mynewfriend');
    

  • destroy() discontinues friendship with the user specified in the id parameter and the authenticating user.

    Exemple 819. Deleting a friend

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->friendships->destroy('myoldfriend');
    

Search Methods

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

    Exemple 820. Searching for tweets

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->search->tweets('#zendframework');
    

    The tweets() method accepts an optional second argument, an array of optional parameters to modify the query.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

    • lang indicates which two-letter language code to restrict results to.

    • locale indicates which two-letter language code is being used in the query.

    • geocode can be used to indicate the geographical radius in which tweets should originate; the string should be in the form "latitude,longitude,radius", with "radius" being a unit followed by one of "mi" or "km".

    • result_type indicates what type of results to retrieve, and should be one of "mixed," "recent," or "popular."

    • until can be used to specify a the latest date for which to return tweets.

Status Methods

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

    Exemple 821. Retrieving public timeline

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->statuses->sample();
    

  • homeTimeline() returns the 20 most recent statuses posted by the authenticating user and that user's friends.

    Exemple 822. Retrieving the home timeline

    $twitter  = new Zend_Service_Twitter($options);
    ));
    $response = $twitter->statuses->homeTimeline();
    

    The homeTimeline() method accepts an array of optional parameters to modify the query.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • trim_user, when set to boolean true, "t", or 1, will list the author identifier only in embedded user objects in the statuses returned.

    • contributor_details, when set to boolean true, will return the screen name of any contributors to a status (instead of only the contributor identifier).

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

    • exclude_replies controls whether or not status updates that are in reply to other statuses will be returned.

  • userTimeline() returns the 20 most recent statuses posted from the authenticating user.

    Exemple 823. Retrieving user timeline

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->statuses->userTimeline();
    

    The userTimeline() method accepts an array of optional parameters to modify the query.

    • user_id specifies the ID of the user for whom to return the timeline.

    • screen_name specifies the screen name of the user for whom to return the timeline.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • trim_user, when set to boolean true, "t", or 1, will list the author identifier only in embedded user objects in the statuses returned.

    • contributor_details, when set to boolean true, will return the screen name of any contributors to a status (instead of only the contributor identifier).

    • include_rts controls whether or not to include native retweets in the returned list.

    • exclude_replies controls whether or not status updates that are in reply to other statuses will be returned.

  • show() returns a single status, specified by the id parameter below. The status' author will be returned inline.

    Exemple 824. Showing user status

    $twitter  = new Zend_Service_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.

    Exemple 825. Updating user status

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->statuses->update('My Great Tweet');
    

    The update() method accepts a second additional parameter.

    • inReplyToStatusId specifies the ID of an existing status that the status to be posted is in reply to.

  • mentionsTimeline() returns the 20 most recent @replies (status updates prefixed with @username) for the authenticating user.

    Exemple 826. Showing user replies

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->statuses->mentionsTimeline();
    

    The mentionsTimeline() method accepts an array of optional parameters to modify the query.

    • since_id narrows the returned results to just those statuses after the specified identifier (up to 24 hours old).

    • max_id narrows the returned results to just those statuses earlier than the specified identifier.

    • count specifies the number of statuses to return, up to 200.

    • trim_user, when set to boolean true, "t", or 1, will list the author identifier only in embedded user objects in the statuses returned.

    • contributor_details, when set to boolean true, will return the screen name of any contributors to a status (instead of only the contributor identifier).

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.

  • destroy() destroys the status specified by the required id parameter.

    Exemple 827. Deleting user status

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->statuses->destroy(12345);
    

User Methods

  • show() returns extended information of a given user, specified by ID or screen name as per the required id parameter below.

    Exemple 828. Showing user informations

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->users->show('myfriend');
    

  • search() will search for users matching the query provided.

    Exemple 829. Searching for users

    $twitter  = new Zend_Service_Twitter($options);
    $response = $twitter->users->search('Zend');
    

    The search() method accepts an array of optional parameters to modify the query.

    • count specifies the number of statuses to return, up to 20.

    • page specifies the page of results to return, based on the count provided.

    • include_entities controls whether or not entities, which includes URLs, mentioned users, and hashtags, will be returned.