SYNCER

SYNCER

POST statuses/update - ツイートを投稿する

9件

公開日:

statuses/update(POSTメソッド)は、ツイートを投稿するエンドポイントです。media/uploadでアップロードしたメディアのIDを指定することで、画像や動画を添付することもできます。

POST statuses/update
POST statuses/update
Twitterの公式リファレンス(英語)。statuses/updateに関する説明があります。

リソース

エンドポイント

POST https://api.twitter.com/1.1/statuses/update.json

条件

項目ユーザー認証アプリケーション認証
利用×
パーミッションRead and Write-
レートリミットあり(Twitterルールを適用)-

パラメータ

status

指定例: APIを利用してツイートを投稿しました。この投稿は削除予定です。 投稿元: https://syncer.jp/Web/API/Twitter/REST_API/POST/statuses/update/

ツイートの内容。URLアドレスやハッシュタグには、自動でリンクが貼られます。

in_reply_to_status_id

指定例: 599624452280754178

ツイートIDを指定すると、そのツイートに対するリプライになる。

possibly_sensitive

指定例: false

trueにすることで、不適切な可能性のある画像のフラグを付けられる。media_idsプロパティで画像を投稿する際にのみ、考慮する項目。

lat

指定例: 35.79449997305192

緯度。ツイートに位置情報を付ける時にlongと併せて指定する。

long

指定例: 139.79078800000002

経度。ツイートに位置情報を付ける時にlatと併せて指定する。

place_id

指定例: df51dec6f4ee2b2c

場所のID。緯度、経度ではなく、特定の場所を指定して、ツイートに位置座標を付与する。

display_coordinates

指定例: false

ツイートに位置座標を付与する時、ピンポイントの位置座標を付与するか否か。falseを指定すると、大まかな範囲(Bounding box)の位置座標のみが付与されて、プライバシーが守られる。

trim_user

指定例: true

ユーザーオブジェクトを除外するか。trueなら除外する、falseなら除外しない。

media_ids

指定例: 471592142565957632

メディアのID。media/uploadのエンドポイントで画像や動画をアップロードした時に取得できる。複数指定するには、カンマ(,)で区切る。画像は4つまで、GIFと動画は1つまで指定可能。

レスポンス

アクセストークン(OAuth1.0)でリクエストをした場合の例です。

リクエストURL

https://api.twitter.com/1.1/statuses/update.json

リクエストヘッダー

Authorization: OAuth display_coordinates=false,lat=35.79449997305192,long=139.79078800000002,media_ids=822340860096323584,oauth_consumer_key=:consumer_key,oauth_nonce=0.72089700+1484903214,oauth_signature_method=HMAC-SHA1,oauth_timestamp=1484903214,oauth_token=:access_token,oauth_version=1.0,place_id=df51dec6f4ee2b2c,status=API%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6%E3%83%84%E3%82%A4%E3%83%BC%E3%83%88%E3%82%92%E6%8A%95%E7%A8%BF%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%E3%81%93%E3%81%AE%E6%8A%95%E7%A8%BF%E3%81%AF%E5%89%8A%E9%99%A4%E4%BA%88%E5%AE%9A%E3%81%A7%E3%81%99%E3%80%82%0D%0A%0D%0A%E6%8A%95%E7%A8%BF%E5%85%83%3A%0D%0Ahttps%3A%2F%2Fsyncer.jp%2FWeb%2FAPI%2FTwitter%2FREST_API%2FPOST%2Fstatuses%2Fupdate%2F,trim_user=false,oauth_signature=pvtzB8R%2B17rhhxkj7cwmn%2F6dvLo%3D

リクエストボディ

status=API%E3%82%92%E5%88%A9%E7%94%A8%E3%81%97%E3%81%A6%E3%83%84%E3%82%A4%E3%83%BC%E3%83%88%E3%82%92%E6%8A%95%E7%A8%BF%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%E3%81%93%E3%81%AE%E6%8A%95%E7%A8%BF%E3%81%AF%E5%89%8A%E9%99%A4%E4%BA%88%E5%AE%9A%E3%81%A7%E3%81%99%E3%80%82%0D%0A%0D%0A%E6%8A%95%E7%A8%BF%E5%85%83%3A%0D%0Ahttps%3A%2F%2Fsyncer.jp%2FWeb%2FAPI%2FTwitter%2FREST_API%2FPOST%2Fstatuses%2Fupdate%2F&lat=35.79449997305192&long=139.79078800000002&place_id=df51dec6f4ee2b2c&display_coordinates=false&trim_user=false&media_ids=822340860096323584

レスポンスボディ (JSON)

JSON

{"created_at":"Fri Jan 20 09:06:54 +0000 2017","id":822369804136763393,"id_str":"822369804136763393","text":"API\u3092\u5229\u7528\u3057\u3066\u30c4\u30a4\u30fc\u30c8\u3092\u6295\u7a3f\u3057\u307e\u3057\u305f\u3002\u3053\u306e\u6295\u7a3f\u306f\u524a\u9664\u4e88\u5b9a\u3067\u3059\u3002\n\n\u6295\u7a3f\u5143:\nhttps:\/\/t.co\/7yw3Z929bb https:\/\/t.co\/WC7pGYgh6G","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/7yw3Z929bb","expanded_url":"https:\/\/syncer.jp\/Web\/API\/Twitter\/REST_API\/POST\/statuses\/update\/","display_url":"syncer.jp\/Web\/API\/Twitte\u2026","indices":[39,62]}],"media":[{"id":822340860096323584,"id_str":"822340860096323584","indices":[63,86],"media_url":"http:\/\/pbs.twimg.com\/media\/C2mKp8JUoAAX3f4.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C2mKp8JUoAAX3f4.jpg","url":"https:\/\/t.co\/WC7pGYgh6G","display_url":"pic.twitter.com\/WC7pGYgh6G","expanded_url":"https:\/\/twitter.com\/arayutw\/status\/822369804136763393\/photo\/1","type":"photo","sizes":{"large":{"w":100,"h":100,"resize":"fit"},"thumb":{"w":100,"h":100,"resize":"crop"},"small":{"w":100,"h":100,"resize":"fit"},"medium":{"w":100,"h":100,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":822340860096323584,"id_str":"822340860096323584","indices":[63,86],"media_url":"http:\/\/pbs.twimg.com\/media\/C2mKp8JUoAAX3f4.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/C2mKp8JUoAAX3f4.jpg","url":"https:\/\/t.co\/WC7pGYgh6G","display_url":"pic.twitter.com\/WC7pGYgh6G","expanded_url":"https:\/\/twitter.com\/arayutw\/status\/822369804136763393\/photo\/1","type":"photo","sizes":{"large":{"w":100,"h":100,"resize":"fit"},"thumb":{"w":100,"h":100,"resize":"crop"},"small":{"w":100,"h":100,"resize":"fit"},"medium":{"w":100,"h":100,"resize":"fit"}}}]},"source":"\u003ca href=\"https:\/\/syncer.jp\/\" rel=\"nofollow\"\u003eSYNCER APPS WRITE\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":564,"listed_count":113,"created_at":"Tue Jun 18 17:28:51 +0000 2013","favourites_count":4459,"utc_offset":32400,"time_zone":"Asia\/Tokyo","geo_enabled":true,"verified":false,"statuses_count":34914,"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\/821911941396328448\/VjorK4J1.jpg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/821911941396328448\/VjorK4J1.jpg","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/821941553774010370\/5M2umxbl_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/821941553774010370\/5M2umxbl_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1528352858\/1484800383","profile_link_color":"D36015","profile_sidebar_border_color":"F2E195","profile_sidebar_fill_color":"FFF7CC","profile_text_color":"0C3E53","profile_use_background_image":true,"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":{"id":"df51dec6f4ee2b2c","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/df51dec6f4ee2b2c.json","place_type":"neighborhood","name":"Presidio","full_name":"Presidio, San Francisco","country_code":"US","country":"United States","contained_within":[],"bounding_box":{"type":"Polygon","coordinates":[[[-122.4891333,37.786925],[-122.446306,37.786925],[-122.446306,37.8128675],[-122.4891333,37.8128675]]]},"attributes":{}},"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":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: 4654
content-type: application/json;charset=utf-8
date: Fri, 20 Jan 2017 09:06:55 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Fri, 20 Jan 2017 09:06:55 GMT
pragma: no-cache
server: tsa_m
set-cookie: lang=ja; Path=/
set-cookie: guest_id=v1%3A148490321484751145; Domain=.twitter.com; Path=/; Expires=Sun, 20-Jan-2019 09:06:55 UTC
status: 200 OK
strict-transport-security: max-age=631138519
x-access-level: read-write
x-connection-hash: 88e013e2bbfe88fc021c4db4befd94eb
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 443
x-transaction: 00984ae300be1d42
x-tsa-request-body-time: 1
x-twitter-response-tags: BouncerCompliant
x-xss-protection: 1; mode=block

デモ

statuses/updateのリクエストを試すことができます。ユーザー認証のデモを試す場合は、アプリケーション(書込権限)の連携を許可する必要があります。利用後、お手数ですが連携の解除をお願いします。連携を解除し忘れたとしても、当アプリケーションが許可なくデータにアクセスすることはありません。

status

in_reply_to_status_id

possibly_sensitive

/

lat

long

place_id

display_coordinates

/

trim_user

/

media_ids

認証方法 (ユーザー認証のみ)

/

サンプルコード

statuses/updateのエンドポイントを利用するためのサンプルプログラムです。各種トークンの情報をセットしてからPHPで起動して下さい。リクエストに成功すると、$jsonという変数に目的のデータが代入されます。このデータを整形するなり保存するなり、活用して下さい。

アクセストークン

PHP (OAuth1.0)

<?php
/**************************************************

	[POST statuses/update]のお試しプログラム

	認証方式: アクセストークン

	配布: SYNCER
	公式ドキュメント: https://dev.twitter.com/rest/reference/post/statuses/update
	日本語解説ページ: https://syncer.jp/Web/API/Twitter/REST_API/POST/statuses/update/

**************************************************/

	// 設定
	$api_key = '' ;	// APIキー
	$api_secret = '' ;	// APIシークレット
	$access_token = '' ;	// アクセストークン
	$access_token_secret = '' ;	// アクセストークンシークレット
	$request_url = 'https://api.twitter.com/1.1/statuses/update.json' ;	// エンドポイント
	$request_method = 'POST' ;

	// パラメータA (オプション)
	$params_a = array(
		"status" => "APIを利用してツイートを投稿しました。この投稿は削除予定です。

投稿元:
https://syncer.jp/Web/API/Twitter/REST_API/POST/statuses/update/",
//		"in_reply_to_status_id" => "599624452280754178",
//		"possibly_sensitive" => "false",
//		"lat" => "35.79449997305192",
//		"long" => "139.79078800000002",
//		"place_id" => "df51dec6f4ee2b2c",
//		"display_coordinates" => "false",
//		"trim_user" => "true",
//		"media_ids" => "471592142565957632",
	) ;

	// キーを作成する (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の末尾に追加 (POSTの場合は不要)
//	if ( $params_a ) {
//		$request_url .= '?' . http_build_query( $params_a ) ;
//	}

	// オプションがある場合、コンテキストにPOSTフィールドを作成する
	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'] ) ) {
		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;">POST statuses/update</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 .= 	'<h3>ボディ</h3>' ;
	$html .= 	'<p><textarea style="width:80%" rows="8">' . ( isset($context['http']['content']) ? $context['http']['content'] : "" ) . '</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/POST/statuses/update/" target="_blank">SYNCER</a></small>' ;

	// 出力 (本稼働時はHTMLのヘッダー、フッターを付けよう)
	echo $html ;