質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

1391閲覧

twitter api を使用してtwitter画像を取得したい

take45

総合スコア30

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

1クリップ

投稿2021/06/24 00:45

PHPでtwitter API + TwitterOAuthを使用してキーワードにあった投稿を取得し、
webサイトに掲載したいと考えています。

投稿の全体は取得できたのですが、
各投稿に表示されている画像や動画のオブジェクトがどこにあるのかわかりません。

色々なサイトで調べたところ、entities->urlsにあるようなことが書かれているのですが、
実際にアクセスしてみても見つけることができません。

各投稿の画像や動画の取得方法を教えていただけるようお願いいたします。

php

1 2require 'vendor/autoload.php'; 3use Abraham\TwitterOAuth\TwitterOAuth; 4 5$consumer_key = '**************'; 6$consumer_secret = '**************'; 7$access_token = '**************'; 8$access_token_secret = '**************'; 9 10 11$connection = new TwitterOAuth( $consumer_key, $consumer_secret, $access_token, $access_token_secret ); 12 13 14$hash_params = ['count' => 20,'q' => '#ニュース', 'lang'=>'ja']; 15$tweets = $connection->get('search/tweets', $hash_params)->statuses; 16$list = []; 17for($i = 0 ; $i < count($tweets) ; $i++){ 18 $url = 'https://twitter.com/sj/status/'.$tweets[$i]->id;,//投稿URL 19 20 $list[$i]=[ 21 'date' => $tweets[$i]->created_at,//投稿日 22 'screen_name'=>'@'.$tweets[$i]->user->screen_name,//ユーザーネーム 23 'id' => $tweets[$i]->id,//投稿ID 24 'txt' => $tweets[$i]->text,//投稿内容 25 'url' => $url, 26 'user_image' => $tweets[$i]->user->profile_image_url_https,//ユーザーイメージ 27 ]; 28 } 29 var_dump( $list); 30 31 var_dump( $tweets); 32 33

下記実際に取得されたソース($twitter)の一部です

php

1 2 3array (size=20) 4 0 => 5 object(stdClass)[7] 6 public 'created_at' => string 'Thu Jun 24 00:40:14 +0000 2021' (length=30) 7 public 'id' => int 1407861087723155457 8 public 'id_str' => string '1407861087723155457' (length=19) 9 public 'text' => string '京都市、医療・介護等の統合データを活用した分析事業を推進[#ニュース] 10https://t.co/0dIqeJ6xe6' (length=123) 11 public 'truncated' => boolean false 12 public 'entities' => 13 object(stdClass)[17] 14 public 'hashtags' => 15 array (size=1) 16 ... 17 public 'symbols' => 18 array (size=0) 19 ... 20 public 'user_mentions' => 21 array (size=0) 22 ... 23 public 'urls' => 24 array (size=1) 25 ... 26 public 'metadata' => 27 object(stdClass)[19] 28 public 'iso_language_code' => string 'ja' (length=2) 29 public 'result_type' => string 'recent' (length=6) 30 public 'source' => string '<a href="https://mobile.twitter.com" rel="nofollow">Twitter Web App</a>' (length=71) 31 public 'in_reply_to_status_id' => null 32 public 'in_reply_to_status_id_str' => null 33 public 'in_reply_to_user_id' => null 34 public 'in_reply_to_user_id_str' => null 35 public 'in_reply_to_screen_name' => null 36 public 'user' => 37 object(stdClass)[20] 38 public 'id' => int 1341034975177523200 39 public 'id_str' => string '1341034975177523200' (length=19) 40 public 'name' => string 'デジタル行政' (length=18) 41 public 'screen_name' => string 'digitalgyosei' (length=13) 42 public 'location' => string '' (length=0) 43 public 'description' => string 'デジタル化を旗振る全ての行政職員向け情報サイト。国・自治体による行政のデジタル化事例をテーマにした情報を、インタビューやニュース情報として提供。' (length=216) 44 public 'url' => string 'https://t.co/y7ELiGpqm9' (length=23) 45 public 'entities' => 46 object(stdClass)[23] 47 ... 48 public 'protected' => boolean false 49 public 'followers_count' => int 42 50 public 'friends_count' => int 9 51 public 'listed_count' => int 0 52 public 'created_at' => string 'Mon Dec 21 14:57:54 +0000 2020' (length=30) 53 public 'favourites_count' => int 2 54 public 'utc_offset' => null 55 public 'time_zone' => null 56 public 'geo_enabled' => boolean false 57 public 'verified' => boolean false 58 public 'statuses_count' => int 90 59 public 'lang' => null 60 public 'contributors_enabled' => boolean false 61 public 'is_translator' => boolean false 62 public 'is_translation_enabled' => boolean false 63 public 'profile_background_color' => string 'F5F8FA' (length=6) 64 public 'profile_background_image_url' => null 65 public 'profile_background_image_url_https' => null 66 public 'profile_background_tile' => boolean false 67 public 'profile_image_url' => string 'http://pbs.twimg.com/profile_images/1341068542330896389/OC1h1TuN_normal.png' (length=75) 68 public 'profile_image_url_https' => string 'https://pbs.twimg.com/profile_images/1341068542330896389/OC1h1TuN_normal.png' (length=76) 69 public 'profile_banner_url' => string 'https://pbs.twimg.com/profile_banners/1341034975177523200/1609125668' (length=68) 70 public 'profile_link_color' => string '1DA1F2' (length=6) 71 public 'profile_sidebar_border_color' => string 'C0DEED' (length=6) 72 public 'profile_sidebar_fill_color' => string 'DDEEF6' (length=6) 73 public 'profile_text_color' => string '333333' (length=6) 74 public 'profile_use_background_image' => boolean true 75 public 'has_extended_profile' => boolean true 76 public 'default_profile' => boolean true 77 public 'default_profile_image' => boolean false 78 public 'following' => boolean false 79 public 'follow_request_sent' => boolean false 80 public 'notifications' => boolean false 81 public 'translator_type' => string 'none' (length=4) 82 public 'withheld_in_countries' => 83 array (size=0) 84 ... 85 public 'geo' => null 86 public 'coordinates' => null 87 public 'place' => null 88 public 'contributors' => null 89 public 'is_quote_status' => boolean false 90 public 'retweet_count' => int 0 91 public 'favorite_count' => int 0 92 public 'favorited' => boolean false 93 public 'retweeted' => boolean false 94 public 'possibly_sensitive' => boolean false 95 public 'lang' => string 'ja' (length=2) 96 1 => 97 98

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

あるやん・・・

public 'entities' => object(stdClass)[17] public 'hashtags' => array (size=1) ... public 'symbols' => array (size=0) ... public 'user_mentions' => array (size=0) ... public 'urls' => array (size=1)

投稿2021/06/24 00:52

deo_deo

総合スコア198

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

take45

2021/06/24 02:34

説明が足らず申し訳ございませんでした var_dump($$tweets->entities->urls)をすると 4つのオブジェクトがあり public 'url' public 'expanded_url' public 'display_url' public 'indices' //配列 なのですが全て画像データではありませんでした。 'indices'配列の値を何かしらするのかとおもったのですが よくわかりませんでした。  どうすれば画像データを取得できるかを ご教授いただけると助かります。
deo_deo

2021/06/24 02:43

その前に一旦確認したいのですが、「画像データ」というのはURLを指していますか? それとも「画像バイナリ」を指してますか?
take45

2021/06/24 02:54

度々すみません URLです。 よろしくお願いします
deo_deo

2021/06/24 03:02

軽く調べてみたのですが、投稿に紐づく画像や動画等がある場合にのみ extended_entities というキー名でレスポンスされるみたいですね。 なので、投稿に画像等のメディアファイルが添付されていない場合はextended_entities は存在しないかと思います。 一応ドキュメントを載せておきます。 https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/extended-entities
take45

2021/06/24 03:41

ありがとうございます。 Twiiterの公式サイト確認しました。 無事取得できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問