GET statuses/home_timeline - ホームタイムラインを取得する
公開日:
statuses/home_timeline(GETメソッド)は、ホームタイムラインのツイートを取得するエンドポイントです。
- GET statuses/home_timeline
- Twitterの公式リファレンス(英語)。statuses/home_timelineに関する説明があります。
リソース
エンドポイント
GET https://api.twitter.com/1.1/statuses/home_timeline.json
条件
項目 | ユーザー認証 | アプリケーション認証 |
---|---|---|
利用 | ● | × |
パーミッション | Read | - |
レートリミット | 15 / 15min | - |
パラメータ
count
指定例: 10
結果の数。1
〜200
の間で指定する。
since_id
指定例: 643299864344788992
ページングに利用する。ツイートのIDを指定すると、これを含まず、これより未来のツイートを取得できる。
max_id
指定例: 643299864344788992
ページングに利用する。ツイートのIDを指定すると、これを含まず、これより過去のツイートを取得できる。
trim_user
指定例: true
ユーザーオブジェクトを除外するか。true
なら除外する、false
なら除外しない。
exclude_replies
指定例: true
リプライを除外するか。true
なら除外する、false
なら除外しない。
include_entities
指定例: true
ツイートオブジェクト内のentitiesプロパティを含めるか否か。
レスポンス
アクセストークン(OAuth1.0)でリクエストをした場合の例です。
リクエストURL
https://api.twitter.com/1.1/statuses/home_timeline.json?count=5&trim_user=false
リクエストヘッダー
Authorization: OAuth count=5,oauth_consumer_key=:consumer_key,oauth_nonce=0.89344700+1484719720,oauth_signature_method=HMAC-SHA1,oauth_timestamp=1484719720,oauth_token=:access_token,oauth_version=1.0,trim_user=false,oauth_signature=eysmveG%2F2H4eI7%2BVZITP7CRTrXk%3D
レスポンスボディ (JSON)
JSON
[{"created_at":"Wed Jan 18 06:06:03 +0000 2017","id":821599514108760065,"id_str":"821599514108760065","text":"\u4e2d\u592e\u5e02\u5834\u306b\u65e2\u306b6000\u5104\u3082\u7a0e\u91d1\u304c\u6295\u5165\u3055\u308c\u3066\u3066\u3001\u5730\u4e0b\u6c34\u3092\u98f2\u6599\u6c34\u306e\u57fa\u6e96\u3067\u8003\u3048\u305f\u3089\u306a\u3093\u304b\u5206\u304b\u3089\u3093\u3051\u3069\u5371\u306a\u3044\u304b\u3089\u79fb\u8ee2\u3057\u305f\u304f\u306a\u3044\u3068\u3044\u3046\u95a2\u4fc2\u8005\u306e\u6620\u50cf\u3092\u898b\u3066\u3001\n\u3082\u3046\u3053\u308c\u3001\u4e2d\u306e\u4eba\u9054\u3082\u5f15\u304d\u969b\u304c\u308f\u304b\u3089\u306a\u304f\u306a\u3063\u3066\u308b\u3093\u304b\u306a\u3041\u2026\u3068\n\n6000\u5104\u3042\u308c\u3070\u90fd\u5185\u306e\u79c1\u7acb\u9ad8\u6821\u306e\u6388\u696d\u6599\u3092100\u5e74\u307b\u3069\u7121\u511f\u5316\u53ef\u80fd\u3063\u307d\u3044\u304c\u2026","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca href=\"http:\/\/sites.google.com\/site\/yorufukurou\/\" rel=\"nofollow\"\u003eYoruFukurou\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":30881611,"id_str":"30881611","name":"\u30b3\u30e9\u30fc\u30b2\u30f3\u305f\u3063\u3077\u308a\u3055\u3093","screen_name":"uupaa","location":"","description":"\u56fd\u5185\u3067\u7e41\u6b96\u3057\u305f\u30e1\u30ad\u30b7\u30b3\u30b5\u30e9\u30de\u30f3\u30c0\u30fc\u3055\u3093\u3060\u3088\u3002\u990c\u3068\u9593\u9055\u3048\u3066\u5c0f\u6307\u3092\u7518\u565b\u307f\u3059\u308b\u53ef\u611b\u3044\u3084\u3064\u3060\u3088\uff01 \u8105\u5a01\u306e\u751f\u547d\u529b\u3068\u3001\u5fae\u7b11\u307f\u3092\u517c\u306d\u5099\u3048\u305f\u51c4\u3044\u3084\u3064\u3060\u3088\uff01\u7d30\u304b\u3044\u3053\u3068\u306f\u3001\u304a\u5bdf\u3057\u304f\u3060\u3055\u3044\u306a\u3093\u3060\u3088\uff01","url":"http:\/\/t.co\/mb4JXLIVwE","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/mb4JXLIVwE","expanded_url":"http:\/\/d.hatena.ne.jp\/uupaa\/","display_url":"d.hatena.ne.jp\/uupaa\/","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":2899,"friends_count":1744,"listed_count":257,"created_at":"Mon Apr 13 15:09:03 +0000 2009","favourites_count":3620,"utc_offset":32400,"time_zone":"Tokyo","geo_enabled":false,"verified":false,"statuses_count":49451,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"0099B9","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1296768888\/uupaa_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1296768888\/uupaa_normal.jpg","profile_link_color":"0099B9","profile_sidebar_border_color":"5ED4DC","profile_sidebar_fill_color":"95E8EC","profile_text_color":"3C3940","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Wed Jan 18 05:54:27 +0000 2017","id":821596596018311173,"id_str":"821596596018311173","text":"(\u301c\u30fb\u25c7\u30fb)\u301c \u30b4\u30f3\u2026 \u304a\u307e\u3048\u30d0\u30b0\u3060\u3063\u305f\u306e\u304b\u2026","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca href=\"http:\/\/sites.google.com\/site\/yorufukurou\/\" rel=\"nofollow\"\u003eYoruFukurou\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":30881611,"id_str":"30881611","name":"\u30b3\u30e9\u30fc\u30b2\u30f3\u305f\u3063\u3077\u308a\u3055\u3093","screen_name":"uupaa","location":"","description":"\u56fd\u5185\u3067\u7e41\u6b96\u3057\u305f\u30e1\u30ad\u30b7\u30b3\u30b5\u30e9\u30de\u30f3\u30c0\u30fc\u3055\u3093\u3060\u3088\u3002\u990c\u3068\u9593\u9055\u3048\u3066\u5c0f\u6307\u3092\u7518\u565b\u307f\u3059\u308b\u53ef\u611b\u3044\u3084\u3064\u3060\u3088\uff01 \u8105\u5a01\u306e\u751f\u547d\u529b\u3068\u3001\u5fae\u7b11\u307f\u3092\u517c\u306d\u5099\u3048\u305f\u51c4\u3044\u3084\u3064\u3060\u3088\uff01\u7d30\u304b\u3044\u3053\u3068\u306f\u3001\u304a\u5bdf\u3057\u304f\u3060\u3055\u3044\u306a\u3093\u3060\u3088\uff01","url":"http:\/\/t.co\/mb4JXLIVwE","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/mb4JXLIVwE","expanded_url":"http:\/\/d.hatena.ne.jp\/uupaa\/","display_url":"d.hatena.ne.jp\/uupaa\/","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":2899,"friends_count":1744,"listed_count":257,"created_at":"Mon Apr 13 15:09:03 +0000 2009","favourites_count":3620,"utc_offset":32400,"time_zone":"Tokyo","geo_enabled":false,"verified":false,"statuses_count":49451,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"0099B9","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme4\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1296768888\/uupaa_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1296768888\/uupaa_normal.jpg","profile_link_color":"0099B9","profile_sidebar_border_color":"5ED4DC","profile_sidebar_fill_color":"95E8EC","profile_text_color":"3C3940","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"ja"}]
レスポンスヘッダー
HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-disposition: attachment; filename=json.json
content-length: 6306
content-type: application/json;charset=utf-8
date: Wed, 18 Jan 2017 06:08:41 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Wed, 18 Jan 2017 06:08:41 GMT
pragma: no-cache
server: tsa_m
set-cookie: lang=ja; Path=/
set-cookie: guest_id=v1%3A148471972098093540; Domain=.twitter.com; Path=/; Expires=Fri, 18-Jan-2019 06:08:41 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read
x-connection-hash: 2d6c5d0f0cd4af1b0241e9c2b58bcb8f
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-rate-limit-limit: 15
x-rate-limit-remaining: 12
x-rate-limit-reset: 1484720566
x-response-time: 173
x-transaction: 0012887300d19cf1
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block
デモ
statuses/home_timelineのリクエストを試すことができます。ユーザー認証のデモを試す場合は、アプリケーション(読取権限)の連携を許可する必要があります。利用後、お手数ですが連携の解除をお願いします。連携を解除し忘れたとしても、当アプリケーションが許可なくデータにアクセスすることはありません。
サンプルコード
statuses/home_timelineのエンドポイントを利用するためのサンプルプログラムです。各種トークンの情報をセットしてからPHPで起動して下さい。リクエストに成功すると、$json
という変数に目的のデータが代入されます。このデータを整形するなり保存するなり、活用して下さい。
アクセストークン
PHP (OAuth1.0)
<?php
/**************************************************
[GET statuses/home_timeline]のお試しプログラム
認証方式: アクセストークン
配布: SYNCER
公式ドキュメント: https://dev.twitter.com/rest/reference/get/statuses/home_timeline
日本語解説ページ: https://syncer.jp/Web/API/Twitter/REST_API/GET/statuses/home_timeline/
**************************************************/
// 設定
$api_key = '' ; // APIキー
$api_secret = '' ; // APIシークレット
$access_token = '' ; // アクセストークン
$access_token_secret = '' ; // アクセストークンシークレット
$request_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json' ; // エンドポイント
$request_method = 'GET' ;
// パラメータA (オプション)
$params_a = array(
// "count" => "10",
// "since_id" => "643299864344788992",
// "max_id" => "643299864344788992",
// "trim_user" => "true",
// "exclude_replies" => "true",
// "include_entities" => "true",
) ;
// キーを作成する (URLエンコードする)
$signature_key = rawurlencode( $api_secret ) . '&' . rawurlencode( $access_token_secret ) ;
// パラメータB (署名の材料用)
$params_b = array(
'oauth_token' => $access_token ,
'oauth_consumer_key' => $api_key ,
'oauth_signature_method' => 'HMAC-SHA1' ,
'oauth_timestamp' => time() ,
'oauth_nonce' => microtime() ,
'oauth_version' => '1.0' ,
) ;
// パラメータAとパラメータBを合成してパラメータCを作る
$params_c = array_merge( $params_a , $params_b ) ;
// 連想配列をアルファベット順に並び替える
ksort( $params_c ) ;
// パラメータの連想配列を[キー=値&キー=値...]の文字列に変換する
$request_params = http_build_query( $params_c , '' , '&' ) ;
// 一部の文字列をフォロー
$request_params = str_replace( array( '+' , '%7E' ) , array( '%20' , '~' ) , $request_params ) ;
// 変換した文字列をURLエンコードする
$request_params = rawurlencode( $request_params ) ;
// リクエストメソッドをURLエンコードする
// ここでは、URL末尾の[?]以下は付けないこと
$encoded_request_method = rawurlencode( $request_method ) ;
// リクエストURLをURLエンコードする
$encoded_request_url = rawurlencode( $request_url ) ;
// リクエストメソッド、リクエストURL、パラメータを[&]で繋ぐ
$signature_data = $encoded_request_method . '&' . $encoded_request_url . '&' . $request_params ;
// キー[$signature_key]とデータ[$signature_data]を利用して、HMAC-SHA1方式のハッシュ値に変換する
$hash = hash_hmac( 'sha1' , $signature_data , $signature_key , TRUE ) ;
// base64エンコードして、署名[$signature]が完成する
$signature = base64_encode( $hash ) ;
// パラメータの連想配列、[$params]に、作成した署名を加える
$params_c['oauth_signature'] = $signature ;
// パラメータの連想配列を[キー=値,キー=値,...]の文字列に変換する
$header_params = http_build_query( $params_c , '' , ',' ) ;
// リクエスト用のコンテキスト
$context = array(
'http' => array(
'method' => $request_method , // リクエストメソッド
'header' => array( // ヘッダー
'Authorization: OAuth ' . $header_params ,
) ,
) ,
) ;
// パラメータがある場合、URLの末尾に追加
if( $params_a ) {
$request_url .= '?' . http_build_query( $params_a ) ;
}
// オプションがある場合、コンテキストにPOSTフィールドを作成する (GETの場合は不要)
// if( $params_a ) {
// $context['http']['content'] = http_build_query( $params_a ) ;
// }
// cURLを使ってリクエスト
$curl = curl_init() ;
curl_setopt( $curl, CURLOPT_URL , $request_url ) ;
curl_setopt( $curl, CURLOPT_HEADER, 1 ) ;
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST , $context['http']['method'] ) ; // メソッド
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER , false ) ; // 証明書の検証を行わない
curl_setopt( $curl, CURLOPT_RETURNTRANSFER , true ) ; // curl_execの結果を文字列で返す
curl_setopt( $curl, CURLOPT_HTTPHEADER , $context['http']['header'] ) ; // ヘッダー
// if( isset( $context['http']['content'] ) && !empty( $context['http']['content'] ) ) { // GETの場合は不要
// curl_setopt( $curl , CURLOPT_POSTFIELDS , $context['http']['content'] ) ; // リクエストボディ
// }
curl_setopt( $curl , CURLOPT_TIMEOUT , 5 ) ; // タイムアウトの秒数
$res1 = curl_exec( $curl ) ;
$res2 = curl_getinfo( $curl ) ;
curl_close( $curl ) ;
// 取得したデータ
$json = substr( $res1, $res2['header_size'] ) ; // 取得したデータ(JSONなど)
$header = substr( $res1, 0, $res2['header_size'] ) ; // レスポンスヘッダー (検証に利用したい場合にどうぞ)
// [cURL]ではなく、[file_get_contents()]を使うには下記の通りです…
// $json = file_get_contents( $request_url , false , stream_context_create( $context ) ) ;
// JSONをオブジェクトに変換
$obj = json_decode( $json ) ;
// HTML用
$html = '' ;
// タイトル
$html .= '<h1 style="text-align:center; border-bottom:1px solid #555; padding-bottom:12px; margin-bottom:48px; color:#D36015;">GET statuses/home_timeline</h1>' ;
// エラー判定
if( !$json || !$obj ) {
$html .= '<h2>エラー内容</h2>' ;
$html .= '<p>データを取得することができませんでした…。設定を見直して下さい。</p>' ;
}
// 検証用
$html .= '<h2>取得したデータ</h2>' ;
$html .= '<p>下記のデータを取得できました。</p>' ;
$html .= '<h3>ボディ(JSON)</h3>' ;
$html .= '<p><textarea style="width:80%" rows="8">' . $json . '</textarea></p>' ;
$html .= '<h3>レスポンスヘッダー</h3>' ;
$html .= '<p><textarea style="width:80%" rows="8">' . $header . '</textarea></p>' ;
// 検証用
$html .= '<h2>リクエストしたデータ</h2>' ;
$html .= '<p>下記内容でリクエストをしました。</p>' ;
$html .= '<h3>URL</h3>' ;
$html .= '<p><textarea style="width:80%" rows="8">' . $context['http']['method'] . ' ' . $request_url . '</textarea></p>' ;
$html .= '<h3>ヘッダー</h3>' ;
$html .= '<p><textarea style="width:80%" rows="8">' . implode( "\r\n" , $context['http']['header'] ) . '</textarea></p>' ;
// フッター
$html .= '<small style="display:block; border-top:1px solid #555; padding-top:12px; margin-top:72px; text-align:center; font-weight:700;">プログラムの説明: <a href="https://syncer.jp/Web/API/Twitter/REST_API/GET/statuses/home_timeline/" target="_blank">SYNCER</a></small>' ;
// 出力 (本稼働時はHTMLのヘッダー、フッターを付けよう)
echo $html ;