Phirehoseを使っているんだけどTwitterのストリーミングAPI(Streamed Tweets)によるつぶやき一覧が取得できない。
目次
前回、GitHubのPhirehose(新しいタブで開く)というライブラリを使用して、TwitterのストリーミングAPI(Streamed Tweets)による、つぶやき一覧を取得しようとしましたができませんでした。
前回のおさらい
GitHubからPhirehoseをダウンロードすると、exampleというフォルダがあるので、その中にあるfilter-oauth.phpというファイルをコピペして、新しいファイルを作成します。ここでは、s_tweets.phpとしておきましょう。
s_tweets.phpでは、最初にPhirehoseのlibフォルダ内にある、Phirehose.phpとOauthPhirehose.phpを読み込みます。
これらのファイルとフォルダをstreamingフォルダにまとめ、任意のサーバーにアップロードします。streamingフォルダの構成は、以下の通りとなります。
streaming:. ├─lib │ Phirehose.php │ OauthPhirehose.php │ s_tweets.php
なお、s_tweets.phpのコードは、以下の通りです。
<?php require_once('./lib/Phirehose.php'); require_once('./lib/OauthPhirehose.php'); /** * Example of using Phirehose to display a live filtered stream using track words */ class FilterTrackConsumer extends OauthPhirehose { /** * Enqueue each status * * @param string $status */ public function enqueueStatus($status) { /* * In this simple example, we will just display to STDOUT rather than enqueue. * NOTE: You should NOT be processing tweets at this point in a real application, instead they should be being * enqueued and processed asyncronously from the collection process. */ $data = json_decode($status, true); if (is_array($data) && isset($data['user']['screen_name'])) { print $data['user']['screen_name'] . ': ' . urldecode($data['text']) . "\n"; } } } // The OAuth credentials you received when registering your app at Twitter define("TWITTER_CONSUMER_KEY", "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○"); define("TWITTER_CONSUMER_SECRET", "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○"); // The OAuth data for the twitter account define("OAUTH_TOKEN", "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○"); define("OAUTH_SECRET", "○○○○○○○○○○○○○○○○○○○○○○○○○○○○○○"); // Start streaming $sc = new FilterTrackConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER); $sc->setFollow(array('山手線')); $sc->setLang("ja"); $sc->consume();
コードの問題ではなく通信上の問題か?
ここまでの作業を行い、http://www.example.com/streaming/s_tweetsにアクセスしても画面遷移しません。タブのファビコンが、ぐるぐる回るだけの状態になります。
個人的な推測ですが、streamingフォルダ内の構成や、s_tweets.phpのコードが間違っているような感じはしません。
どちらかというと、サーバー同士の通信(HTTP)に問題があるような気がします。ただその通信が上手くいっているのかどうか、調べる方法が分からないんですが…。
【2014.09.13追記】
今回の記事を書くにあたって使用したファイルとフォルダは、GitHubにアップロードしています。ご興味のある方はこちら(新しいタブで開く)にアクセスしてください。
〔つづきの記事〕
〔参考サイト〕