LoginSignup
24
18

More than 5 years have passed since last update.

Twitterの特定のユーザーのフォロワー数を取得する(だけ)PHP

Last updated at Posted at 2014-10-22

1.アプリ登録

v1.1からOauthによる認証が必要なためアプリ登録して
consumerKey、consumerSecret、accessToken、accessTokenSecretの値をメモる

参考:
PHPでOAuth認証して自分のつぶやきを表示する [Twitter API 1.1 対応版]

2.ライブラリダウンロード

abraham/twitteroauth
のライブラリを使うのでダウンロード

twitteroauthのフォルダのOAuth.php、twitteroauth.phpのみ使用する

3.実装

consumerKey、consumerSecret、accessToken、accessTokenSecretを以下に打ち込む

require_once("twitteroauth/twitteroauth.php");
$consumerKey = 'xxxx';
$consumerSecret = 'xxxx';
$accessToken = 'xxxx';
$accessTokenSecret = 'xxxx';

$screen_name = "hoge";

$twObj = new TwitterOAuth($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);
$res = $twObj->get('users/show', compact('screen_name'));
if (isset($res->followers_count)) {
    echo $res->followers_count;
} else if (isset($res->errors[0]->message)) {
    echo $res->errors[0]->message;
} else {
    echo 'Unknown Error';
}
$oObj = json_decode($res,true);
echo $oObj['followers_count'];

screen_nameは取得したいユーザーID

以上

参考

公式API仕様
https://dev.twitter.com/rest/reference/get/users/show

Twitterのフォロワー数取得について
http://choice-site.com/2013/07/11/twitter%E3%81%AE%E3%83%95%E3%82%A9%E3%83%AD%E3%83%AF%E3%83%BC%E6%95%B0%E5%8F%96%E5%BE%97%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6/

TwitterOAuthの正しい使い方
http://qiita.com/rana_kualu/items/357a031c0453a3538ad3

[PHP] ライブラリに頼らないTwitterAPI入門
http://qiita.com/mpyw/items/b59d3ce03f08be126000

24
18
2

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
24
18