Introduction au Zend Framework

 Apprendre Zend Framework

appendix

 Guide de référence Zend Framework


  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 61.2% Update 2010-11-28 - Revision 23149 - Version ZF 1.11.x

    63.33. Zend_Service_Twitter

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

    63.33.2. 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 63.150. 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();

    63.33.3. Account Methods

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

      Exemple 63.151. Verifying credentials

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->account->verifyCredentials();

    • endSession() signs users out of client-facing applications.

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

      Exemple 63.153. Rating limit status

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->account->rateLimitStatus();

    63.33.4. Status Methods

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

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

      Exemple 63.155. 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 specifies which page you want to return.

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

      Exemple 63.156. 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 specifies 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' author will be returned inline.

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

      Exemple 63.158. 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 specifies 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.

      Exemple 63.159. 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 specifies 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.

      Exemple 63.160. Deleting user status

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->status->destroy(12345);

    63.33.5. User Methods

    • friends()r eturns up to 100 of the authenticating user's friends who have most recently updated, each with current status inline.

      Exemple 63.161. 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 specifies which page you want to return.

    • followers() returns the authenticating user's followers, each with current status inline.

      Exemple 63.162. 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 specifies 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.

      Exemple 63.163. Showing user informations

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->user->show('myfriend');

    63.33.6. Direct Message Methods

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

      Exemple 63.164. 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 specifies which page you want to return.

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

      Exemple 63.165. 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 specifies 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.

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

      Exemple 63.167. Deleting direct message

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->directMessage->destroy(123548);

    63.33.7. Friendship Methods

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

      Exemple 63.168. 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 and the authenticating user.

      Exemple 63.169. Deleting friend

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->friendship->destroy('myoldfriend');

    • exists() tests if a friendship exists between the user specified in the id parameter and the authenticating user.

      Exemple 63.170. Checking friend existence

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->friendship->exists('myfriend');

    63.33.8. Favorite Methods

    • favorites() returns the 20 most recent favorite statuses for the authenticating user or user specified by the id parameter.

      Exemple 63.171. Retrieving favorites

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->favorite->favorites();

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

      • id specifies the ID or screen name of the user for whom to request a list of favorite statuses.

      • page specifies which page you want to return.

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

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

      Exemple 63.173. Deleting favorites

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->favorite->destroy(12351);

    63.33.9. Block Methods

    • exists() checks if the authenticating user is blocking a target user and can optionally return the blocked user's object if a block does exists.

      Exemple 63.174. Checking if block exists

      $twitter = new Zend_Service_Twitter('myusername''mysecretpassword');
      // returns true or false
      $response $twitter->block->exists('blockeduser');
      // returns the blocked user's info if the user is blocked
      $response2 $twitter->block->exists('blockeduser'true);

      The favorites() method accepts a second optional parameter.

      • returnResult specifies whether or not return the user object instead of just TRUE or FALSE.

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

      Exemple 63.175. Blocking a user

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->block->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 63.176. Removing a block

      $twitter    = new Zend_Service_Twitter('myusername''mysecretpassword');
      $response   $twitter->block->destroy('blockeduser');

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

      Exemple 63.177. Who are you blocking

      $twitter = new Zend_Service_Twitter('myusername''mysecretpassword');
      // return the full user list from the first page
      $response $twitter->block->blocking();
      // return an array of numeric user IDs from the second page
      $response2 $twitter->block->blocking(2true);

      The favorites() method accepts two optional parameters.

      • page specifies which page ou want to return. A single page contains 20 IDs.

      • returnUserIds specifies whether to return an array of numeric user IDs the authenticating user is blocking instead of an array of user objects.

    63.33.10. Zend_Service_Twitter_Search

    63.33.10.1. Introduction

    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.

    63.33.10.2. Twitter Trends

    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.

    $twitterSearch  = new Zend_Service_Twitter_Search();
    $twitterTrends  $twitterSearch->trends();

    foreach (
    $twitterTrends as $trend) {
        print 
    $trend['name'] . ' - ' $trend['url'] . PHP_EOL
    }

    The return array has two values in it:

    • name is the name of trend.

    • url is the URL to see the tweets for that trend.

    63.33.10.3. Searching Twitter

    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 is the number of tweets to return per page, up to a maximum of 100.

    • page specifies the page number to return, up to a maximum of roughly 1500 results (based on rpp * page).

    • since_id returns tweets with status IDs greater than the given ID.

    • show_user specifies whether to add ">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).

    Exemple 63.178. JSON Search Example

    The following code sample will return an array with the search results.

    $twitterSearch  = new Zend_Service_Twitter_Search('json');
    $searchResults  $twitterSearch->search('zend', array('lang' => 'en'));

    Exemple 63.179. ATOM Search Example

    The following code sample will return a Zend_Feed_Atom object.

    $twitterSearch  = new Zend_Service_Twitter_Search('atom');
    $searchResults  $twitterSearch->search('zend', array('lang' => 'en'));

    63.33.10.4. Zend-specific Accessor Methods

    While the Twitter Search API only specifies two methods, Zend_Service_Twitter_Search has additional methods that may be used for retrieving and modifying internal properties.

    • getResponseType() and setResponseType() allow you to retrieve and modify the response type of the search between JSON and Atom.

    digg delicious meneame google twitter technorati facebook

    Comments

    Loading...