The Blueprint
I had a little personal project where I needed to combine my wife’s and my Twitter feed into one common feed. This was so we could display it on our blog and it would show in realtime what we were doing. Not a problem using Yahoo Pipes since our accounts are unprotected and open to the public. I created a simple pipe to combine the feeds and sort by date.
This past week I had another personal project where we have a large music festival with lots of people that will be twittering while the event is going on. Many of them have their account protected (for whatever reason) and short of asking them to unprotect it for the week of the festival, there is no easy way to get access to their feed. I couldn’t even get access to it for a few days because once you open your feed to the public anyone can see all your history.
What I needed was a way to grab their twitter feed only during the festival so I put my brain in overdrive and shortly after came up with a pretty elegant way of doing it. I had remembered using a script called Snoopy that is a PHP class that simulates a web browser. I dug through the Twitter API and found the “friends_timeline” call, which is basically a feed of all my friend’s tweets (what you see on twitter.com/home). I put the 2 together and had the core of a script that would let me login to Twitter and pull the tweets for everyone I follow.
There are two ways to go from here. You could create a dedicated Twitter account and follow just the people you want to display on the overall feed. Or you can create an array of usernames in your code that is used to filter only the ones you want in the overall feed. In my final code I choose to go with the later for simplicity.
Here is the core of what I wrote to pull in the feed via XML
require_once('snoopy.class.php');
$snoopy = new Snoopy;
$snoopy->agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
$snoopy->user = 'your_twitter_username';
$snoopy->pass = 'your_twitter_password';
$snoopy->fetch('http://twitter.com/statuses/friends_timeline.xml');
// Make sure we connected to the API
if (strpos($snoopy->response_code, '200')) {
// Use PHP's SimpleXML functions
$xml = simplexml_load_string($snoopy->results);
foreach($xml->status as $status) {
// Do your code here using the format:
// $status->id
// $status->user->screen_name
// etc...
}
}
There are a few things to note:
1) The main thing is that if someone has their Twitter marked as “protected”, it is that way for a reason. Be SURE you have their permission to pull their tweets only during a set time of your event or whatever you are doing.
2) The Twitter API will only display the latest 20 tweets. To get around this I created a PHP file using the above code and I run it on a Cron Job every 1 minute, dumping the results in a database table. Then I pull from that DB table when I display the feed. Here is the complete file that I use (I added the Snoopy class so it would be self contained): twitter_grab_cron.zip
Search The Blog
Code & Projects
Categories
Archives
- April 2010
- October 2009
- September 2009
- August 2009
- June 2009
- May 2009
- April 2009
- February 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- April 2008
- March 2008
- February 2008
- July 2007
When i try this script. I get a blank page. Do you know why?
without a look at your code I don’t. You have to replace some usernames and passwords in there with your data. The script isn’t exactly “plud and play” as you have to change a few things and connect it to your DB if that is what you want to do. Also the cron script is designed to not have any output… it just insert data into the DB. You’ll have to write whatever scripts you need to get it to output.
thats great that you are talking about the twitter api,a good example of searching with the twitter api is on twiogle.com because you can search on twitter and google at the same time.
Why are you so brilliant? I have no idea how to use that. I’d love a user-friendly method for viewing protected tweets, but short of cloning your brain cells and implanting them in my head, I don’t think this method is going to work for me.
How do I use this?
@Autumn what part are you having a issue with?