Last active
June 23, 2018 14:50
-
-
Save among753/5823942 to your computer and use it in GitHub Desktop.
Application-only auth に対応したtwitteroauth.phpを使用してtwitter search API を叩く例
twitteroauth.phpは下記のGitHubのソースを使用しないと動きません。
GitHub: https://github.com/among753/twitteroauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Load required lib files. */ | |
require_once('twitteroauth/twitteroauth.php');// Application-only auth に対応したものを使うこと | |
//require_once('config.php'); | |
define('CONSUMER_KEY', 'xxxxxxxxxxxxxxxxxxxx'); | |
define('CONSUMER_SECRET', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); | |
header("content-type: text/html; charset=utf-8"); | |
var_dump($_GET); | |
$q = (isset($_GET['q'])) ? $_GET['q'] : "なう"; | |
$param = array("q" => urlencode($q), "count" => "20", "lang" => "ja"); | |
/* Create a TwitterOauth object with consumer/my application tokens. */ | |
//$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET);// Twitter Developersから取得したアクセストークンを使用 | |
/* Create a TwitterOauth object with consumer */ | |
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);// コンシューマーキーのみセット | |
/* Proxy Setting */ | |
//$connection->setProxy(PROXY_URL);// プロキシ内からのアクセスの場合セット | |
/* OAuth 2 Bearer Token */ | |
$connection->getBearerToken();// OAuth 2.0のBearer Tokenを使う | |
$result = $connection->get('search/tweets', $param);// GETでsearch APIにリクエストを送る | |
var_dump($connection->http_header['x_rate_limit_remaining']);// 残り回数表示 | |
foreach($result->statuses as $status){ | |
echo '<li>'; | |
echo '<p class="twitter_icon"><a href="http://twitter.com/'.$status->user->screen_name.'" target="_blank"><img src="'.$status->user->profile_image_url.'" alt="icon" width="46" height="46" /></a></p>'; | |
echo '<div class="twitter_tweet"><p><span class="twitter_content">'.$status->text.'</span><span class="twitter_date">'.$status->created_at.'</span></p></div>'; | |
echo "</li>\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment