1

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;
}
?>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Nagy
  • 11
  • 1
  • 3
  • I'v spend a few days overcoming all the issues that are active for the twitter realtime api and in the end I'v developed my own solution.Here is the project on GitHub with a detailed explanation how to setup the base project https://github.com/tech-387/Tech-387-Twitter-Streaming-Interface – DaAmidza Dec 03 '17 at 14:29

4 Answers4

3

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;
}
?>
muffinista
  • 6,676
  • 2
  • 30
  • 23
  • It is very useful. Could you pinpoint me of how to use that in a php script. How can I consume the tweets and parse the json lines. I tried to call this link instead of the one called in the upper script but it did not work. Thanks in advance. My plan is to work with that for sometime then switch to OAuth so no need to address the OAuth part now. Thanks in advance – Nagy Jun 04 '11 at 08:48
  • I've never used PHP and Twitter together, but googling suggests that phirehose will do the trick: https://github.com/fennb/phirehose – muffinista Jun 04 '11 at 19:48
1

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.

linvi
  • 115
  • 1
  • 7
0

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();
  ?>
MIRMIX
  • 1,052
  • 2
  • 14
  • 40
0

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.

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
DWRoelands
  • 4,878
  • 4
  • 29
  • 42