Twitter Search API (2)
My web app requires users to cast votes via twitter a开发者_JAVA技巧nd display those votes in a line chart. Id like to accomplish this whole thing using JavaScript.
My Requirements:
- Using the twitter API, I need to Collect votes in real time and have those votes displayed in a line chart, in real time!
Using the twitter API, I need to Collect votes in real time and have those votes displayed in a line chart, in real time!
If you're doing real time stuff, you want to use the Streaming API. It's pretty easy to get up and running quickly using existing open-source client implementations like Phirehose. It comes with some great examples too.
Here's their example that tracks a bunch of keywords: you'd probably want to track whatever hashtag people are voting on:
<?php
require_once('../lib/Phirehose.php');
/**
* Example of using Phirehose to display a live filtered stream using track words
*/
class FilterTrackConsumer extends Phirehose
{
/**
* 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";
}
}
}
// Start streaming
$sc = new FilterTrackConsumer('username', 'password', Phirehose::METHOD_FILTER);
$sc->setTrack(array('morning', 'goodnight', 'hello', 'the'));
$sc->consume();
If you're low volume, you could probably just shove the tweet right into the database in enqueueStatus.
Have you checked the Twitter-API itself?
http://dev.twitter.com/doc
The documentation is pretty verbose and contains also some examples on how to use it with JavaScript. (probably you will have to sing in or sign up to get access to the api, I am not sure).
精彩评论