Onga inc

subtitle

Twitter API

Twitter_logo_blue

Twitter API

  • APIキーの取得
  • 特定のユーザーの投稿を取得する
  • 特定ユーザーのフォロワー一覧を取得する
  • 特定のユザーをフォローする
  • アプリケーションの認証を行う

特定のユーザーの投稿を取得する

APIキーの取得

APIを使用するためには、以下のページからAPIキーを取得する必要があります。
以下の情報が必要になります。
oauth_access_token
oauth_access_token_secret
consumer_key
consumer_secret

https://dev.twitter.com/

スクリーンショット 2014-12-17 22.14.11

以下のプラグインを使用して、プラグインからtwitterのAPIを使用します。

TwitterAPIExchange

https://github.com/J7mbo/twitter-api-php/blob/master/TwitterAPIExchange.php

TwitterAPIExchange.phpを指定のディレクトリに設置して、
実行PHPファイルから読み込んでください。

ユーザーIDを確認するサイト

http://gettwitterid.com/?user_name=onga_inc&submit=GET+USER+ID

特定のユーザーのタイムラインを取得する

GET statuses/user_timeline

`

$settings = array(
    'oauth_access_token' => "アクセストークン",
    'oauth_access_token_secret' => "アクセストークンシークレット",
    'consumer_key' => "カスタマーキー",
    'consumer_secret' => "カスタマーキーシークレット"
);

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/followers/ids.json';
$getfield = '?screen_name=[スクリーンネーム]';//スクリーンネームを指定する。
$requestMethod = 'GET';

$twitter = new \TwitterAPIExchange($settings); //TwitterAPIExchangeをnewする。
$res = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

$res=json_decode($res);

`

特定のユーザーのフォロワー一覧を取得する。

GET followers/ids

`

$settings = array(
    'oauth_access_token' => "アクセストークン",
    'oauth_access_token_secret' => "アクセストークンシークレット",
    'consumer_key' => "カスタマーキー",
    'consumer_secret' => "カスタマーキーシークレット"
);

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/followers/ids.json';
$getfield = '?user_id=220332457';//ユーザーIDを指定する
$requestMethod = 'GET';

$twitter = new \TwitterAPIExchange($settings); //TwitterAPIExchangeをnewする。
$res = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

$res=json_decode($res);

`

特定のユーザーを自動フォローする。

POST friendships/create

`

$settings = array(
    'oauth_access_token' => "アクセストークン",
    'oauth_access_token_secret' => "アクセストークンシークレット",
    'consumer_key' => "カスタマーキー",
    'consumer_secret' => "カスタマーキーシークレット"
);

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/statuses/followers/ids.json';
$postfields = array(
     'user_id' => $id,//ユーザーIDを指定する
     'follow'  => 'true'
);
$requestMethod = 'POST';

$twitter = new \TwitterAPIExchange($settings); //TwitterAPIExchangeをnewする。
$res = $twitter->setPostfields($postfields)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

$res=json_decode($res);

`

Onga

A designer, developer and entrepreneur. Spends his time travelling the world with a bag of kites. Likes journalism and publishing platforms.

Comments