Consuming twitter stream API
I need to consume the real time tweets sample. I have this piece of code but i could not get anything ma i calling the right link. definitely i put the right username and password but still it did not go through. Please advice is it because twitter changed the API recently. Sorry if 开发者_开发问答the question is basic I am new to that. Thanks in advance
<?php
//datacollector.php
$fp =
fopen("http://username:pass@stream.twitter.com/spritzer.json
while($data = fgets($fp))
{
$time = date("YmdH");
if ($newTime!=$time)
{
@fclose($fp2);
$fp2 = fopen("{$time}.txt","a");
}
fputs($fp2,$data);
$newTime = $time;
}
?>
What DWRoelands posts is true about deprecation, but that's not actually your problem -- the problem is that Twitter deprecated spritzer.json. You should instead use /1/statuses/sample.json
to get the firehose. Here's a command-line example that works:
curl http://stream.twitter.com/1/statuses/sample.json -uusername:password
If you're looking to use this code in production for a long time, I would certainly consider implementing OAuth, but since Twitter hasn't even announced a schedule for turning off Basic Auth, there's no rush if you're just fooling around. Of course, they could turn it off tomorrow without any warning.
Here's some working PHP code, just replace username:password with actual credentials:
<?php
//datacollector.php
$fp = fopen("http://username:password@stream.twitter.com/1/statuses/sample.json", "r");
while($data = fgets($fp)) {
$time = date("YmdH");
if ($newTime!=$time) {
@fclose($fp2);
$fp2 = fopen("{$time}.txt","a");
}
fputs($fp2,$data);
$newTime = $time;
}
?>
If you need an application you can develop easily with C# using Twitter C# API : Tweetinvi
The API is providing a class Stream that will allow you to retrieve the information of the stream by using a delegate function.
Here is the example provided by the samples:
private static void streaming_example(Token token)
{
// Creating a Delegate to use processTweet function to analyze each Tweet coming from the stream
Stream.ProcessTweetDelegate produceTweetDelegate = new Stream.ProcessTweetDelegate(processTweet);
// Creating the stream and specifying the delegate
Stream myStream = new Stream(produceTweetDelegate);
// Starting the stream by specifying credentials thanks to the Token
myStream.StartStream(token);
}
Easy and fast to implement.
I am one of the developer working on the project.
The streaming API documentation indicates that you need to validate with OAuth in order to access user streams. See: Twitter: Streaming API Concepts
That page also indicates that Basic Authentication is going to be deprecated, so I think you'll need to adapt your code to use OAuth.
Download lib folder from here.Put them inside the same folder with this code below. Modify Api keys and change keyword you want to track. If you gonna execute it in browser wait a bit.
<?php
/***************libraries*********************/
require_once('../lib/Phirehose.php');
require_once('../lib/OauthPhirehose.php');
/*********Escape from Execution Time***********/
set_time_limit(0);
class FilterTrackConsumer extends OauthPhirehose
{
public function enqueueStatus($status)
{
$data = json_decode($status, true);
if (is_array($data) && isset($data['user']['screen_name'])) {
print $data['text'];
}
}
}
/*************************API KEYS*******************************/
define('TWITTER_CONSUMER_KEY', 'api_key');
define('TWITTER_CONSUMER_SECRET','api_secret');
define('OAUTH_TOKEN','token');
define('OAUTH_SECRET','token secret');?>
// Start streaming
$sc = new FilterTrackConsumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_FILTER);
$sc->setTrack(array('#keyword'));
$sc->consume();
?>
精彩评论