GET statuses/retweets/:id - ツイートをRTしたユーザーをオブジェクトで取得する
公開日:
statuses/retweets/:id(GETメソッド)は、対象ツイートをリツイートしたユーザーを、オブジェクトの一覧で取得するエンドポイントです。
- GET statuses/retweets/:id
- Twitterの公式リファレンス(英語)。statuses/retweets/:idに関する説明があります。
リソース
エンドポイント
GET https://api.twitter.com/1.1/statuses/retweets/:id.json
条件
項目 | ユーザー認証 | アプリケーション認証 |
---|---|---|
利用 | ● | ● |
パーミッション | Read | - |
レートリミット | 75 / 15min | 300 / 15min |
パラメータ
:id
指定例: 599624452280754178
ツイートのID。エンドポイントのURLに含めること。
count
指定例: 10
結果の数。1
〜100
の間で指定する。
trim_user
指定例: true
ユーザーオブジェクトを除外するか。true
なら除外する、false
なら除外しない。
レスポンス
アクセストークン(OAuth1.0)でリクエストをした場合の例です。
リクエストURL
https://api.twitter.com/1.1/statuses/retweets/599624452280754178.json?count=4&trim_user=false
リクエストヘッダー
Authorization: OAuth count=4,oauth_consumer_key=:consumer_key,oauth_nonce=0.31396900+1484726492,oauth_signature_method=HMAC-SHA1,oauth_timestamp=1484726492,oauth_token=:access_token,oauth_version=1.0,trim_user=false,oauth_signature=YmhOf7yGbm33YUcgfGjny6LUBXw%3D
レスポンスボディ (JSON)
JSON
[{"created_at":"Thu Oct 27 06:13:30 +0000 2016","id":791523195849289730,"id_str":"791523195849289730","text":"RT @arayutw: \u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"arayutw","name":"\u3042\u3089\u3086","id":1528352858,"id_str":"1528352858","indices":[3,11]}],"urls":[]},"source":"\u003ca href=\"https:\/\/syncer.jp\/twitter-api-matome\" rel=\"nofollow\"\u003eSyncer Write Permission\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":3381483372,"id_str":"3381483372","name":"\u7d05\u6d41@\u3082\u3082\u3093\u304c","screen_name":"momonga_9R","location":"","description":"\u97f3\u30b2\u30fc\u3001FGO\u3002\u3042\u3068\u30a8\u30ed\u30b2\u3002maimai\u7d2b\u7406\u8ad6\u5024195\u8b5c\u9762\u3002\u7d2b10\u4ee5\u4e0b\u5168\u7406\u8ad6\u5024\u3002\u8d85\u5c06\u6a59\u5c06\u3002 \u30a2\u30a4\u30b3\u30f3\u88fd\u4f5c\u8005\u69d8\u2192@END_ring","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":263,"friends_count":196,"listed_count":9,"created_at":"Sat Aug 29 12:32:19 +0000 2015","favourites_count":12595,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":24608,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/809369733074038784\/-Z72FHB8_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/809369733074038784\/-Z72FHB8_normal.jpg","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sat May 16 17:16:24 +0000 2015","id":599624452280754178,"id_str":"599624452280754178","text":"\u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":1528352858,"id_str":"1528352858","name":"\u3042\u3089\u3086","screen_name":"arayutw","location":"\u6771\u4eac\u90fd\u8db3\u7acb\u533a","description":"SYNCER\u3068\u3044\u3046\u30b5\u30a4\u30c8\u3092\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u63b2\u793a\u677f\u2192 https:\/\/t.co\/OAIDbACF3N","url":"https:\/\/t.co\/lW3GYq8sQl","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/lW3GYq8sQl","expanded_url":"https:\/\/syncer.jp\/","display_url":"syncer.jp","indices":[0,23]}]},"description":{"urls":[{"url":"https:\/\/t.co\/OAIDbACF3N","expanded_url":"https:\/\/forum.syncer.jp\/","display_url":"forum.syncer.jp","indices":[26,49]}]}},"protected":false,"followers_count":1417,"friends_count":563,"listed_count":114,"created_at":"Tue Jun 18 17:28:51 +0000 2013","favourites_count":4455,"utc_offset":32400,"time_zone":"Asia\/Tokyo","geo_enabled":true,"verified":false,"statuses_count":34815,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":true,"profile_background_color":"2660A1","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_link_color":"D36015","profile_sidebar_border_color":"F2E195","profile_sidebar_fill_color":"FFF7CC","profile_text_color":"0C3E53","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"regular"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":19,"favorite_count":5,"favorited":false,"retweeted":false,"lang":"ja"},"is_quote_status":false,"retweet_count":19,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Thu Oct 20 06:33:47 +0000 2016","id":788991587112005636,"id_str":"788991587112005636","text":"RT @arayutw: \u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"arayutw","name":"\u3042\u3089\u3086","id":1528352858,"id_str":"1528352858","indices":[3,11]}],"urls":[]},"source":"\u003ca href=\"https:\/\/syncer.jp\/twitter-api-matome\" rel=\"nofollow\"\u003eSyncer Write Permission\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":36889290,"id_str":"36889290","name":"\u963f\u90e8","screen_name":"abeu","location":"\u6771\u4eac","description":"\u8077\u696dWEB\u5927\u5de5\u3002","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":7,"friends_count":5,"listed_count":0,"created_at":"Fri May 01 05:11:56 +0000 2009","favourites_count":3,"utc_offset":32400,"time_zone":"Osaka","geo_enabled":false,"verified":false,"statuses_count":95,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/408794718\/ic_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/408794718\/ic_normal.png","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sat May 16 17:16:24 +0000 2015","id":599624452280754178,"id_str":"599624452280754178","text":"\u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":1528352858,"id_str":"1528352858","name":"\u3042\u3089\u3086","screen_name":"arayutw","location":"\u6771\u4eac\u90fd\u8db3\u7acb\u533a","description":"SYNCER\u3068\u3044\u3046\u30b5\u30a4\u30c8\u3092\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u63b2\u793a\u677f\u2192 https:\/\/t.co\/OAIDbACF3N","url":"https:\/\/t.co\/lW3GYq8sQl","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/lW3GYq8sQl","expanded_url":"https:\/\/syncer.jp\/","display_url":"syncer.jp","indices":[0,23]}]},"description":{"urls":[{"url":"https:\/\/t.co\/OAIDbACF3N","expanded_url":"https:\/\/forum.syncer.jp\/","display_url":"forum.syncer.jp","indices":[26,49]}]}},"protected":false,"followers_count":1417,"friends_count":563,"listed_count":114,"created_at":"Tue Jun 18 17:28:51 +0000 2013","favourites_count":4455,"utc_offset":32400,"time_zone":"Asia\/Tokyo","geo_enabled":true,"verified":false,"statuses_count":34815,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":true,"profile_background_color":"2660A1","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_link_color":"D36015","profile_sidebar_border_color":"F2E195","profile_sidebar_fill_color":"FFF7CC","profile_text_color":"0C3E53","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"regular"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":19,"favorite_count":5,"favorited":false,"retweeted":false,"lang":"ja"},"is_quote_status":false,"retweet_count":19,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 18 16:29:47 +0000 2016","id":788416799180136450,"id_str":"788416799180136450","text":"RT @arayutw: \u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"arayutw","name":"\u3042\u3089\u3086","id":1528352858,"id_str":"1528352858","indices":[3,11]}],"urls":[]},"source":"\u003ca href=\"https:\/\/syncer.jp\/twitter-api-matome\" rel=\"nofollow\"\u003eSyncer Write Permission\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":776593628223254532,"id_str":"776593628223254532","name":"T_api","screen_name":"qlc_test_api","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":4,"friends_count":41,"listed_count":0,"created_at":"Fri Sep 16 01:28:44 +0000 2016","favourites_count":68,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":182,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","profile_image_url_https":"https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_5_normal.png","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sat May 16 17:16:24 +0000 2015","id":599624452280754178,"id_str":"599624452280754178","text":"\u30ea\u30c4\u30a4\u30fc\u30c8\u7528\u306e\u30c4\u30a4\u30fc\u30c8\u3067\u3059\u3002\n\u3053\u306e\u30c4\u30a4\u30fc\u30c8\u3092\u597d\u304d\u306a\u3060\u3051\u30ea\u30c4\u30a4\u30fc\u30c8\u3057\u3066\u4e0b\u3055\u3044\u3002","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[]},"source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\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":1528352858,"id_str":"1528352858","name":"\u3042\u3089\u3086","screen_name":"arayutw","location":"\u6771\u4eac\u90fd\u8db3\u7acb\u533a","description":"SYNCER\u3068\u3044\u3046\u30b5\u30a4\u30c8\u3092\u904b\u7528\u3057\u3066\u3044\u307e\u3059\u3002\u63b2\u793a\u677f\u2192 https:\/\/t.co\/OAIDbACF3N","url":"https:\/\/t.co\/lW3GYq8sQl","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/lW3GYq8sQl","expanded_url":"https:\/\/syncer.jp\/","display_url":"syncer.jp","indices":[0,23]}]},"description":{"urls":[{"url":"https:\/\/t.co\/OAIDbACF3N","expanded_url":"https:\/\/forum.syncer.jp\/","display_url":"forum.syncer.jp","indices":[26,49]}]}},"protected":false,"followers_count":1417,"friends_count":563,"listed_count":114,"created_at":"Tue Jun 18 17:28:51 +0000 2013","favourites_count":4455,"utc_offset":32400,"time_zone":"Asia\/Tokyo","geo_enabled":true,"verified":false,"statuses_count":34815,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":true,"profile_background_color":"2660A1","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/632205394824183808\/FIL3S4z4.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/631038167416619008\/7xfGAEwp_normal.png","profile_link_color":"D36015","profile_sidebar_border_color":"F2E195","profile_sidebar_fill_color":"FFF7CC","profile_text_color":"0C3E53","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"regular"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":19,"favorite_count":5,"favorited":false,"retweeted":false,"lang":"ja"},"is_quote_status":false,"retweet_count":19,"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: 15452
content-type: application/json;charset=utf-8
date: Wed, 18 Jan 2017 08:01:32 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Wed, 18 Jan 2017 08:01:32 GMT
pragma: no-cache
server: tsa_m
set-cookie: lang=ja; Path=/
set-cookie: guest_id=v1%3A148472649241392716; Domain=.twitter.com; Path=/; Expires=Fri, 18-Jan-2019 08:01:32 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read
x-connection-hash: 718d9741d6b5f62a115876b8ea21db1b
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-rate-limit-limit: 75
x-rate-limit-remaining: 73
x-rate-limit-reset: 1484727375
x-response-time: 216
x-transaction: 006004f3008440d0
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block
デモ
statuses/retweets/:idのリクエストを試すことができます。ユーザー認証のデモを試す場合は、アプリケーション(読取権限)の連携を許可する必要があります。利用後、お手数ですが連携の解除をお願いします。連携を解除し忘れたとしても、当アプリケーションが許可なくデータにアクセスすることはありません。
サンプルコード
statuses/retweets/:idのエンドポイントを利用するためのサンプルプログラムです。各種トークンの情報をセットしてからPHPで起動して下さい。リクエストに成功すると、$json
という変数に目的のデータが代入されます。このデータを整形するなり保存するなり、活用して下さい。
ベアラートークン
PHP (OAuth2.0)
<?php
/**************************************************
[GET statuses/retweets/:id]のお試しプログラム
認証方式: ベアラートークン
配布: SYNCER
公式ドキュメント: https://dev.twitter.com/rest/reference/get/statuses/retweets/id
日本語解説ページ: https://syncer.jp/Web/API/Twitter/REST_API/GET/statuses/retweeters/id/
**************************************************/
// 設定
$bearer_token = '' ; // ベアラートークン
$request_url = 'https://api.twitter.com/1.1/statuses/retweets/599624452280754178.json' ; // エンドポイント
// パラメータ (オプション)
$params = array(
// "count" => "10",
// "trim_user" => "true",
) ;
// パラメータがある場合
if( $params ) {
$request_url .= '?' . http_build_query( $params ) ;
}
// リクエスト用のコンテキスト
$context = array(
'http' => array(
'method' => 'GET' , // リクエストメソッド
'header' => array( // ヘッダー
'Authorization: Bearer ' . $bearer_token ,
) ,
) ,
) ;
// 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'] ) ; // ヘッダー
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/retweets/:id</h1>' ;
// 検証用
$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/retweeters/id/" target="_blank">SYNCER</a></small>' ;
// 出力 (本稼働時はHTMLのヘッダー、フッターを付けよう)
echo $html ;
アクセストークン
PHP (OAuth1.0)
<?php
/**************************************************
[GET statuses/retweets/:id]のお試しプログラム
認証方式: アクセストークン
配布: SYNCER
公式ドキュメント: https://dev.twitter.com/rest/reference/get/statuses/retweets/id
日本語解説ページ: https://syncer.jp/Web/API/Twitter/REST_API/GET/statuses/retweeters/id/
**************************************************/
// 設定
$api_key = '' ; // APIキー
$api_secret = '' ; // APIシークレット
$access_token = '' ; // アクセストークン
$access_token_secret = '' ; // アクセストークンシークレット
$request_url = 'https://api.twitter.com/1.1/statuses/retweets/599624452280754178.json' ; // エンドポイント
$request_method = 'GET' ;
// パラメータA (オプション)
$params_a = array(
// "count" => "10",
// "trim_user" => "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/retweets/:id</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/retweeters/id/" target="_blank">SYNCER</a></small>' ;
// 出力 (本稼働時はHTMLのヘッダー、フッターを付けよう)
echo $html ;