ItecSoftware Logo

Track And Parse Twitter Messages Stream

Written by Peter Gilg on - like this:
parse twitter stream

With the increasing buzz about Twitter and its messages, more web developers want to incorporate a stream of Twitter messages into their websites.

Ever wanted to listen, track and parse tweets from a Twitter stream from an individual user? It’s quite an easy task, if you know the right URL’s to parse. Curl is a great little tool to get sources from almost any web resource and Curl has roots in Linux command like and PHP, just to name a couple.

Let’s look at how this would work by pulling CNN’s breaking news feed as JSON:

curl http://twitter.com/status/user_timeline/cnnbrk.json

or XML:

curl http://twitter.com/status/user_timeline/cnnbrk.xml

That will give you the last 20 tweets in either JSON or XML format. From here you can parse the format and extract individual messages, time stamp and user names.

An even more interesting option is to get messages where you have been mentioned. It’s a bit more complicated, as we need to supply login credentials, but no rocket science either:

curl -u "username:password" http://www.twitter.com/statuses/mentions.json

(or .xml if you prefer)

That will give you the last few tweets mentioning the user supplied with the curl command. Now all you got to figure out is how to parse the messages and plug them into your application. Keep in mind that Twitter is likely to undergo changes, and so might be the message format and api urls. In addition, expect a limit on the amount of message you can retrieve in a give time frame.

Some hosts require a referrer or cookie to be set. Curl has an extensive set of features to deal with these kind of situations. Should you require them, check out the curl php options page.

Listed in Linux, Shell Scripting, Web Development

Tags: parse twitter feed, tweet, twitter

Leave a Reply

Your email address will not be published.