LoginSignup
0
0

More than 3 years have passed since last update.

TwitterのAPIで高知県の情報を取得してみた。

Posted at

TwitterのAPIで高知県の情報を取得してみた。

条件として

・高知県というワードがあるつぶやき。
・いいね、RTが10件以上、ポチられているツイートを拾う。

使用したライブラリ

TwitterOAuth

phpバージョン

php7.4.x

一言

このままの状態ではとても精度の良い高知県の情報を収集することは出来ない!
ので、工夫が必要です。

common/Twitter_config.php
<?php
define("APIKEY","xxxxxxxx");
define("APISECRET","xxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("ACCESSTOKEN","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
define("ACCESSTOKENSECRET","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

lib/tw_timeline.php
<?php
require 'vendor/autoload.php';
require '../common/Twitter_config.php';

use Abraham\TwitterOAuth\TwitterOAuth;

$consumer_key = APIKEY;
$consumer_secret = APISECRET;
$access_token = ACCESSTOKEN;
$access_token_secret = ACCESSTOKENSECRET;

$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
// result_type
// 指定例: popular
// 取得するツイートの種類。
// popular
// 人気のツイート。
// recent
// 最新のツイート。
// mixed
// 全てのツイート。
$options = [
  'q' => '高知県', 
  'lang' => 'ja',
  'locale' => 'ja',
  'result_type' => 'popular',
  'count' => 100
];

$tweets = $connection->get('search/tweets', $options);

foreach ($tweets->statuses as $key=>$value) {
  if((int)$value->favorite_count >=10 && (int)$value->retweet_count >=10 ){
    print $value->text.PHP_EOL;
    print $value->user->name.PHP_EOL;
    print $value->user->screen_name.PHP_EOL;
    print $value->retweet_count.PHP_EOL;
    print $value->favorite_count.PHP_EOL;

  }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0