views:

594

answers:

5

There is a family of methods (birddog, shadow, and follow)in the Twitter API that opens a (mostly) permanent connection and allows you to follow many users. I've run the sample connection code with cURL in bash, and it works nicely: when a user I specify writes a tweet, I get a stream of XML in my console.

My question is: how can I access data with PHP that isn't returned as a direct function call, but is streamed? This data arrives sporadically and unpredictably, and it's not something I've ever dealt with nor do I know where to begin looking for answers. Any advice and descriptions of libraries or pitfalls would be appreciated.

A: 

I would suggest looking into using AJAX. Im not a PHP developer, but I would think that you could wire up an AJAX call to the API and update your web page.

andrewWinn
+4  A: 

fopen and fgets

<?php
$sock = fopen('http://domain.tld/path/to/file', 'r');
$data = null;
while(($data = fgets($sock)) == TRUE)
{
    echo $data;
}
fclose($sock);

This is by no means great (or even good) code but it should provide the functionality you need. You will need to add error handling and data parsing among other things.

Unkwntech
Note Stream support was added to fopen() in PHP 5.0 so you will need at least that.
Unkwntech
+1  A: 

I'm pretty sure that your script will time out after ~30 seconds of listening for data on the stream. Even if it doesn't, once you get a significant server load, the sheer number of open and listening connections will bring the server to it's knees.

I would suggest you take a look at an AJAX solution that makes a call to a script that just stores a Queue of messages. I'm not sure how the Twitter API works exactly though, so I'm not sure if you can have a script run when requested to get all the tweets, or if you have to have some sort of daemon append the tweets to a Queue that PHP can read and pass back via your AJAX call.

Mike Trpcic
As far as I know though, it's just one connection. Twitter sends me an XML (is it called an object?) per message; my server isn't doing any active querying, only receiving/listening.
Alex Mcp
I would suggest setting up a Daemon that just listens (maybe written in Python, it'd likely be very simple) and dumps all tweets into a MySQL database. Your PHP scripts can then read from the database.
Mike Trpcic
+1  A: 

There are libraries for this these days that make things much easier (and handle the tricky bits like reconnections, socket handling, TCP backoff, etc), ie:

http://code.google.com/p/phirehose/

James Pell
A: 

Phirehose is definitely the way to go:

http://code.google.com/p/phirehose/

Art Peterson