PHPでのTwitter API用のoAuthプロトコルベースのアプリケーションの開発



この記事では、PHPでOAuthプロトコルを使用してTwitter APIを操作する方法について説明します。

OAuthプロトコルは、ユーザー名とパスワードを渡さずにアプリケーションにユーザーデータへのアクセスを提供します。 新しいアプリケーション承認ルールでは、8月31日からTwitterと連携するために「OAuth」テクノロジーを使用する必要があります。

最終的に判明するテストアプリケーションは、ユーザーのメッセージフィード、彼のフォロワーの最新のステータスのフィードを表示できます。各フォロワーまたは友人のステータスの横にあるボタンをクリックすると、このユーザーのフィード全体を読むことができます。



そのため、まずはこのリンクで新しいアプリケーションを作成しください。



これで、config.phpに保存する必要があるデータができました。



define('CONSUMER_KEY', 'Your consumer key');

define('CONSUMER_SECRET', 'You consumer secret');

define('OAUTH_CALLBACK', 'http://yousite.ru/callback.php');








接続ページで、Twitterのconnect.phpで上記のデータを接続し、その可用性を確認します。



if (CONSUMER_KEY === '' || CONSUMER_SECRET === '') {

echo 'You need a consumer key and secret to test the sample code. Get one from https://twitter.com/apps'; exit;}








接続プロセスを開始します(redirect.phpページに移動します)。



$content = 'Sign in with Twitter';

<?php print_r($content); ?>








Twitter APIとアプリケーションアカウントデータを操作するためのライブラリをredirect.phpに接続します



require_once('twitteroauth/twitteroauth.php');

require_once('config.php');








アプリケーションアカウントに基づいてTwitterOAuthオブジェクトを作成します。



$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);







次に、Twitterから一時トークンを取得して保存します。



$_SESSION['oauth_token'] = $token = $request_token['oauth_token'];

$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];








一時的なトークンに基づいて、Twitterでの承認のためにユーザーを送信します。



$url = $connection->getAuthorizeURL($token);







受信したトークンが古い場合、セッションをクリアし、ユーザーをページに送信してTwitterに接続します。



if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {

$_SESSION['oauth_status'] = 'oldtoken';

header('Location: ./clearsessions.php');








clearsessions.phpページで、次を呼び出します。



session_start();

session_destroy();

header('Location: ./connect.php');








すべてうまくいった場合は、アプリケーションアカウントと一時トークンに基づいてTwitterOAuthオブジェクトを作成します。



$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);









次に、Twitterからアクセスキーを取得します。これは将来の使用のために保存する必要があります。



$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

$_SESSION['access_token'] = $access_token;








一時トークンは不要になりました。



unset($_SESSION['oauth_token']);

unset($_SESSION['oauth_token_secret']);








次に、サイトのページに移動します。



header('Location: ./index.php');







index.phpページで、ユーザートークンを変数に保存します。



$access_token = $_SESSION['access_token'];







アプリケーションアカウントとユーザートークンに基づいてTwitterOAuthオブジェクトを作成します。



$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);







これで、さまざまな方法を使用してユーザーデータを取得できます。

たとえば、ユーザーアカウント情報を取得します。



$content_user = $connection->get('account/verify_credentials');

$array_user = (array)$content_user;

$user_id=$array_user["id"];









ユーザーのIDに基づいて、パラメーター配列でidを指定することで、フィードからメッセージを取得できます(パラメーター配列では、表示されるレコードの数も指定できます)。



$content_friends=$connection->get('statuses/friends_timeline',array('id' => $user_id));







すべてのユーザーフォロワーの最近の投稿:



$content_followers=$connection->get('statuses/followers', array('id' => $user_id));







独自のユーザーメッセージフィード(デフォルトでは20エントリのみが表示され、メッセージの最大数は200です)。



$content_userline=$connection->get('statuses/user_timeline', array('id' => $user_id,'count'=>200));







同じ原理で他のメソッドを呼び出すことができます。



一般的なTwitterフィード: api.twitter.com/1/statuses/public_timeline.xml

あなたに書いた公開メッセージ: api.twitter.com/1/statuses/mentions.xml?count=5&page=1

私のリツイート: api.twitter.com/1/statuses/retweeted_by_me.xml?count=5&page=3

私へのリツイート: api.twitter.com/1/statuses/retweeted_to_me.xml?count=5&page=3

その他の方法。



echo print_r($content_friends); //







メッセージフィードを受信した場合の結果は、stdClassオブジェクト要素の配列になります。

[0] => stdClassオブジェクト



[created_at] => Sat Sep 04 11:01:48 +0000 2010

[in_reply_to_screen_name] =>

[ソース] => IncredebleTB

[リツイート] =>

[切り捨て] =>

[in_reply_to_status_id] =>

[in_reply_to_user_id] =>

[貢献者] =>

[場所] =>

[座標] =>

[ユーザー] => stdClassオブジェクト



[show_all_inline_media] =>

[profile_background_image_url] => s.twimg.com/a/1283555538/images/themes/theme16/bg.gif

[favorites_count] => 0

[profile_image_url] => a0.twimg.com/profile_images/1105647496/robot_normal.jpg

[説明] => Webデザイン、Webアプリケーション開発、Webサイト開発

[contributors_enabled] =>

[profile_sidebar_fill_color] => f2f2f2

[url] => dandreev.com

[geo_enabled] =>

[profile_background_tile] =>

[screen_name] =>信じられないほどのTB

[lang] => en

[created_at] =>月4月26日20:32:14 +0000 2010

[profile_sidebar_border_color] => a8a8a8

[場所] =>モスクワ

[検証済み] =>

[follow_request_sent] =>

[続く] => 1

[profile_background_color] => 9AE4E8

[followers_count] => 99

[保護] =>

[profile_text_color] => 141414

[名前] =>ドミトリーアンドレエフ

[listed_count] => 0

[profile_use_background_image] => 1

[time_zone] =>キト

[friends_count] => 48

[id] => 137459247

[statuses_count] => 900

[通知] =>

[utc_offset] => -18000

[profile_link_color] => 0084B4





[ジオ] =>

[retweet_count] =>

[id] => 22967688386

[お気に入り] =>

[text] => Craigslistの検閲:(@techcrunch)を介してアダルトセクションがbit.ly/9evRKNに到着





それらにアクセスするには、たとえば次のように連絡できます。



$array_friends = (array)$content_friends;

foreach ($array_friends as $friends_sets)

{

$friends_sets=(array)$friends_sets; // stdClass Object

$friend_name=$friends_sets["user"]->name; // stdClass Object

}









phpのoAuthのライブラリはここからダウンロードできます

最終的に判明したアプリケーションはこちらです。 アプリケーションには、3つのページ付けテープ(フォロワー、友人、ユーザーの個人用テープ)が含まれています。 さらに、jTweetsAnywhereライブラリが接続されているため、各ユーザーに関する情報を表示できます(上部にある[Twitterで接続]ボタン)。



All Articles