Previous Next

Zend_Service_Twitter

Introduction

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.

Authentication

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.

Example #1 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();

Account Methods

  • verifyCredentials, Use this method to test if supplied user credentials are valid with minimal overhead.

    Example #2 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.

    Example #3 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.

    Example #4 Rating limit status

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

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.

    Example #5 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.

    Example #6 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.

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

    Example #8 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.

    Example #9 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.

    Example #10 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.

    Example #11 Deleting user status

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

User Methods

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

    Example #12 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.

    Example #13 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

    Example #14 Showing user informations

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

Direct Message Methods

  • messages, Returns a list of the 20 most recent direct messages sent to the authenticating user.

    Example #15 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.

    Example #16 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.

    Example #17 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.

    Example #18 Deleting direct message

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

Friendship Methods

  • create, Befriends the user specified in the ID parameter as the authenticating user.

    Example #19 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.

    Example #20 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.

    Example #21 Checking friend existence

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

Favorite Methods

  • favorites, Returns the 20 most recent favorite statuses for the authenticating user or user specified by the ID parameter

    Example #22 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..

    Example #23 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.

    Example #24 Deleting favorites

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

Zend_Service_Twitter_Search

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.

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.

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

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

Example #25 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'));

Example #26 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'));

Zend-specific Accessor Methods

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.

Previous Next
Introduction to Zend Framework
概要
インストール
Zend_Acl
導入
アクセス制御の洗練
高度な使用法
Zend_Amf
導入
Zend_Amf_Server
Zend_Application
導入
Zend_Application Quick Start
Theory of Operation
Examples
コア機能
利用できるリソースプラグイン
Zend_Auth
導入
データベースのテーブルでの認証
ダイジェスト認証
HTTP 認証アダプタ
LDAP 認証
Open ID 認証
Zend_Cache
導入
キャッシュの仕組み
Zend_Cache のフロントエンド
Zend_Cache のバックエンド
Zend_Captcha
導入
Captcha の方法
CAPTCHA アダプタ
Zend_CodeGenerator
導入
Zend_CodeGeneratorサンプル
Zend_CodeGeneratorリファレンス
Zend_Config
導入
動作原理
Zend_Config_Ini
Zend_Config_Xml
Zend_Config_Writer
Zend_Config_Writer
Zend_Console_Getopt
導入
Getopt の規則の宣言
オプションおよび引数の取得
Zend_Console_Getopt の設定
Zend_Controller
Zend_Controller クイックスタート
Zend_Controller の基本
フロントコントローラ
リクエストオブジェクト
標準のルータ
ディスパッチャ
アクションコントローラ
アクションヘルパー
レスポンスオブジェクト
プラグイン
モジュラーディレクトリ構造の規約の使用
MVC での例外
以前のバージョンからの移行
Zend_Currency
Zend_Currency について
通貨の操作方法
以前のバージョンからの移行
Zend_Date
導入
動作原理
基本メソッド
Zend_Date API の概要
日付の作成
日付関数全般用の定数
動作例
Zend_Db
Zend_Db_Adapter
Zend_Db_Statement
Zend_Db_Profiler
Zend_Db_Select
Zend_Db_Table
Zend_Db_Table_Row
Zend_Db_Table_Rowset
導入
Zend_Debug
変数の出力
Zend_Dojo
導入
Zend_Dojo_Data: dojo.data エンベロープ
Dojo ビューヘルパー
Dojoフォーム要素とデコレーター
Zend_Dom
導入
Zend_Dom_Query
Zend_Exception
例外の使用法
Zend_Feed
導入
フィードの読み込み
ウェブページからのフィードの取得
RSS フィードの使用
Atom フィードの使用
単一の Atom エントリの処理
フィードおよびエントリの構造の変更
独自のフィードクラスおよびエントリクラス
Zend_File
Zend_File_Transfer
Zend_File_Transfer 用のバリデータ
Filters for Zend_File_Transfer
以前のバージョンからの移行
Zend_Filter
導入
標準のフィルタクラス群
フィルタチェイン
フィルタの書き方
Zend_Filter_Input
Zend_Filter_Inflector
Zend_Form
Zend_Form
Zend_Form クイックスタート
Zend_Form_Element を用いたフォーム要素の作成
Zend_Form によるフォームの作成
Zend_Form_Decorator による独自のフォームマークアップの作成
Zend Framework に同梱されている標準のフォーム要素
Zend Framework に同梱されている標準のデコレータ
Zend_Form の国際化
Zend_Form の高度な使用法
Zend_Gdata
導入
AuthSub による認証
Using the Book Search Data API
ClientLogin による認証
Google Calendar の使用法
Google Documents List Data API の使用法
Using Google Health
Google Spreadsheets の使用法
Google Apps Provisioning の使用法
Google Base の使用法
Picasa Web Albums の使用法
YouTube Data API の使用法
Gdata の例外処理
Zend_Http
導入
Zend_Http_Client - 高度な使用法
Zend_Http_Client - 接続アダプタ
Zend_Http_Cookie および Zend_Http_CookieJar
Zend_Http_Response
Zend_InfoCard
導入
Zend_Json
導入
基本的な使用法
Zend_Json の高度な使用法
XML から JSON への変換
Zend_Json_Server - JSON-RPCサーバー
Zend_Layout
導入
Zend_Layout クイックスタート
Zend_Layout の設定オプション
Zend_Layout の高度な使用法
Zend_Ldap
導入
Zend_Loader
ファイルやクラスの動的な読み込み
The Autoloader
Resource Autoloaders
プラグインのロード
Zend_Locale
導入
Zend_Locale の使用法
正規化および地域化
日付および時刻の扱い
サポートするロケール
以前のバージョンからの移行
Zend_Log
概要
ライター
フォーマッタ
フィルタ
Zend_Mail
導入
SMTP 経由での送信
SMTP 接続による複数のメールの送信
異なる転送手段の使用
HTML メール
ファイルの添付
受信者の追加
MIME バウンダリの制御
追加のヘッダ
文字セット
エンコーディング
SMTP 認証
セキュアな SMTP トランスポート
メールメッセージの読み込み
Zend_Measure
導入
計測値の作成
計測値の出力
計測値の操作
計測値の型
Zend_Memory
概要
メモリマネージャ
メモリオブジェクト
Zend_Mime
Zend_Mime
Zend_Mime_Message
Zend_Mime_Part
Zend_Navigation
Introduction
画面
Containers
Zend_OpenId
導入
Zend_OpenId_Consumer の基本
Zend_OpenId_Provider
Zend_Paginator
導入
使用法
設定
高度な使用法
Zend_Pdf
導入
PDF ドキュメントの作成および読み込み
PDF ドキュメントへの変更内容の保存
ページの操作
描画
ドキュメントの情報およびメタデータ
Zend_Pdf モジュールの使用例
Zend_ProgressBar
Zend_ProgressBar
Zend_Reflection
導入
Zend_Reflectionサンプル
Zend_Reflectionリファレンス
Zend_Registry
レジストリの使用法
Zend_Rest
導入
Zend_Rest_Client
Zend_Rest_Server
Zend_Search_Lucene
概要
インデックスの構築
インデックスの検索
クエリ言語
クエリ作成用の API
文字セット
拡張性
Java Lucene との相互運用
応用
ベストプラクティス
Zend_Server
導入
Zend_Server_Reflection
Zend_Service
導入
Zend_Service_Akismet
Zend_Service_Amazon
Zend_Service_Amazon_Ec2
Zend_Service_Amazon_Ec2: Instances
Zend_Service_Amazon_Ec2: Windows Instances
Zend_Service_Amazon_Ec2: Reserved Instances
Zend_Service_Amazon_Ec2: CloudWatch Monitoring
Zend_Service_Amazon_Ec2: Amazon Machine Images (AMI)
Zend_Service_Amazon_Ec2: Elastic Block Stroage (EBS)
Zend_Service_Amazon_Ec2: Elastic IP Addresses
Zend_Service_Amazon_Ec2: Keypairs
Zend_Service_Amazon_Ec2: Regions and Availability Zones
Zend_Service_Amazon_Ec2: Security Groups
Zend_Service_Amazon_S3
Zend_Service_Audioscrobbler
Zend_Service_Delicious
Zend_Service_Flickr
Zend_Service_Nirvanix
Zend_Service_ReCaptcha
Zend_Service_Simpy
導入
Zend_Service_StrikeIron
Zend_Service_StrikeIron: バンドルされているサービス
Zend_Service_StrikeIron: 応用編
Zend_Service_Technorati
Zend_Service_Twitter
Zend_Service_Yahoo
Zend_Session
導入
基本的な使用法
高度な使用法
グローバルセッションの管理
Zend_Session_SaveHandler_DbTable
Zend_Soap
Zend_Soap_Server
Zend_Soap_Client
WSDLアクセッサ
自動検出
Zend_Tag
Introduction
Zend_Tag_Cloud
Zend_Test
導入
Zend_Test_PHPUnit
Zend_Text
Zend_Text_Figlet
Zend_Text_Table
Zend_TimeSync
導入
Zend_TimeSync の動作
Zend_Tool_Framework
Introduction
Using the CLI Tool
Architecture
Creating Providers to use with Zend_Tool_Framework
Shipped System Providers
Zend_Tool_Project
Zend_Tool_Project導入
Create A Project
Zend Tool Project Providers
Zend_Translate
導入
Zend_Translate のアダプタ
翻訳アダプタの使用法
以前のバージョンからの移行
Zend_Uri
Zend_Uri
Zend_Validate
導入
標準のバリデーションクラス群
バリデータチェイン
バリデータの書き方
検証メッセージ
Zend_Version
Zend Framework のバージョンの取得
Zend_View
導入
コントローラスクリプト
ビュースクリプト
ビューヘルパー
Zend_View_Abstract
以前のバージョンからの移行
Zend_Wildfire
Zend_Wildfire
Zend_XmlRpc
導入
Zend_XmlRpc_Client
Zend_XmlRpc_Server
Zend Framework のシステム要件
導入
Zend Framework PHP 標準コーディング規約
概要
PHP ファイルの書式
命名規約
コーディングスタイル
Zend Framework Performance Guide
導入
クラスの読み込み
Zend_Dbパフォーマンス
国際化(i18n)とローカライズ(l10n)
ビューのレンダリング
著作権に関する情報