Skip to content

Instantly share code, notes, and snippets.

@mpyw
Created July 25, 2016 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mpyw/fe6741f1348ff63b255d8a6c8d99b405 to your computer and use it in GitHub Desktop.
Save mpyw/fe6741f1348ff63b255d8a6c8d99b405 to your computer and use it in GitHub Desktop.
Twitterのアンケート関連のAPIのサンプル

アンケートAPIのメモ

全部PHPで書いているので他の言語を使うときは読み替えて下さい。Twitter APIのライブラリはTwistOAuthを使っています。
ここで説明するのは公式のAPIからの取得のみです。作成はここからでは出来ません。出来るのは取得のみです。

ログイン

公式のキーでログインする必要があります。サードパーティではアンケートの情報が一切入らないためです。

// Twitter for Macのキーでログイン
$to = TwistOAuth::login("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8", $sn, $pw);

パラメーター

取得でのパラメーターはcards_platforminclude_cardsがあれば取得できます。
include_cardstrueで固定ですが、cards_platformは環境ごとに違うみたいです。
自分が確認した限りでは

  • iPhone-13
  • iPhone-8
  • Android-10

がありました。

// statuses/user_timelineを使ってアンケート情報を取得
$response = $to->get("statuses/user_timeline", ["screen_name" => "hirotoyoyo", "include_cards" => true, "cards_platform" => "iPhone-13"]);

取得

statuses/user_timelineの場合は全部をforeachで回して、その中のcardの存在を確認してcardが存在していればアンケート情報が入っています。
なお、アンケートでは無いツイートはcardが空になっています。

foreach($response as $data) {
  if (isset($data->card)) {
    var_dump($data->card);
  }
  else {
    echo "アンケート情報が無いです。".PHP_EOL;
  }
}
{
"card": {
"name": "poll4choice_text_only",
"url": "card://701763233221185537",
"card_type_url": "http://card-type-url-is-deprecated.invalid",
"binding_values": {
"choice1_label": {
"type": "STRING",
"string_value": "Val 1"
},
"choice2_label": {
"type": "STRING",
"string_value": "Val 2"
},
"selected_choice": {
"type": "STRING",
"string_value": "4"
},
"end_datetime_utc": {
"type": "STRING",
"string_value": "2016-02-23T13:39:27Z"
},
"counts_are_final": {
"type": "BOOLEAN",
"boolean_value": false
},
"choice2_count": {
"type": "STRING",
"string_value": "1"
},
"choice1_count": {
"type": "STRING",
"string_value": "2"
},
"choice4_label": {
"type": "STRING",
"string_value": "Val 4"
},
"last_updated_datetime_utc": {
"type": "STRING",
"string_value": "2016-02-22T13:48:31Z"
},
"duration_minutes": {
"type": "STRING",
"string_value": "1440"
},
"choice3_count": {
"type": "STRING",
"string_value": "1"
},
"choice4_count": {
"type": "STRING",
"string_value": "0"
},
"choice3_label": {
"type": "STRING",
"string_value": "Val 3"
},
"api": {
"type": "STRING",
"string_value": "capi://passthrough/1"
},
"card_url": {
"type": "STRING",
"string_value": "https://twitter.com",
"scribe_key": "card_url"
}
},
"card_platform": {
"platform": {
"device": {
"name": "iPhone",
"version": "13"
},
"audience": {
"name": "production",
"bucket": null
}
}
}
}
}
{
"card": {
"name": "poll4choice_text_only",
"url": "card://701763233221185537",
"card_type_url": "http://card-type-url-is-deprecated.invalid",
"binding_values": {
"choice1_label": {
"type": "STRING",
"string_value": "Val 1"
},
"choice2_label": {
"type": "STRING",
"string_value": "Val 2"
},
"end_datetime_utc": {
"type": "STRING",
"string_value": "2016-02-23T13:39:27Z"
},
"counts_are_final": {
"type": "BOOLEAN",
"boolean_value": false
},
"choice2_count": {
"type": "STRING",
"string_value": "0"
},
"choice1_count": {
"type": "STRING",
"string_value": "0"
},
"choice4_label": {
"type": "STRING",
"string_value": "Val 4"
},
"last_updated_datetime_utc": {
"type": "STRING",
"string_value": "2016-02-22T13:39:45Z"
},
"duration_minutes": {
"type": "STRING",
"string_value": "1440"
},
"choice3_count": {
"type": "STRING",
"string_value": "0"
},
"choice4_count": {
"type": "STRING",
"string_value": "0"
},
"choice3_label": {
"type": "STRING",
"string_value": "Val 3"
},
"api": {
"type": "STRING",
"string_value": "capi://passthrough/1"
},
"card_url": {
"type": "STRING",
"string_value": "https://twitter.com",
"scribe_key": "card_url"
}
},
"card_platform": {
"platform": {
"device": {
"name": "iPhone",
"version": "13"
},
"audience": {
"name": "production",
"bucket": null
}
}
}
}
}
{
"card_uri": "card://701763233221185537",
"status": "OK"
}
<?php
/*
基本的な情報は下のリンクに書いてあります。
https://gist.github.com/Hiroto-K/8a1ace1a871413abd6da#file-api-official-memo-md
*/
require_once __DIR__."/TwistOAuth.phar";
$sn = $argv[1];
$pw = $argv[2];
try {
$to = TwistOAuth::login("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8", $sn, $pw);
$response = $to->get("statuses/lookup", ["id" => "701763234261393409,701104042810830850", "include_cards" => true, "cards_platform" => "iPhone-13"]);
foreach($response as $data) {
if (isset($data->card)) {
var_dump($data->card);
}
else {
echo "アンケート情報が無いです。".PHP_EOL;
}
}
}
catch (Exception $e) {
var_dump($e);
}
<?php
/*
基本的な情報は下のリンクに書いてあります。
https://gist.github.com/Hiroto-K/8a1ace1a871413abd6da#file-api-official-memo-md
*/
require_once __DIR__."/TwistOAuth.phar";
$sn = $argv[1];
$pw = $argv[2];
try {
$to = TwistOAuth::login("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8", $sn, $pw);
$response = $to->get("statuses/show", ["id" => "701763234261393409", "include_cards" => true, "cards_platform" => "iPhone-13"]);
if (isset($response->card)) {
var_dump($response->card);
}
else {
echo "アンケート情報が無いです。".PHP_EOL;
}
}
catch (Exception $e) {
var_dump($e);
}
<?php
/*
基本的な情報は下のリンクに書いてあります。
https://gist.github.com/Hiroto-K/8a1ace1a871413abd6da#file-api-official-memo-md
*/
require_once __DIR__."/TwistOAuth.phar";
$sn = $argv[1];
$pw = $argv[2];
try {
$to = TwistOAuth::login("3rJOl1ODzm9yZy63FACdg", "5jPoQ5kQvMJFDYRNE8bQ4rHuds4xJqhvgNJM4awaE8", $sn, $pw);
$response = $to->get("statuses/user_timeline", ["screen_name" => "hirotoyoyo", "include_cards" => true, "cards_platform" => "iPhone-13"]);
foreach($response as $data) {
if (isset($data->card)) {
var_dump($data->card);
}
else {
echo "アンケート情報が無いです。".PHP_EOL;
}
}
}
catch (Exception $e) {
var_dump($e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment