UPDATE: Check the comments for a version with caching.
A friend of mine recently had a rather unfortunate event involving her twitter public timeline, so I thought the time had come to make mine private, more or less.
As a result, I needed to update the code that pulls my most recent Twitter posts into the left hand column so that it would authenticate using the Twitter API. Here's the new code - it ain't pretty, but it works:
<?php // Your twitter username & password. $username = "YOUR_USERNAME"; $password = "YOUR_TWITTER_PASSWORD"; //Concatenate the username and password $userpass = $username . ":" . $password; //Make up the feed URL $feed = "http://twitter.com/statuses/user_timeline.atom?count=1"; //A function to parse the atom feed and pull out the useful info. function parse_feed($feed, $username) { $stepOne = explode("<content type=\"html\">", $feed); $stepTwo = explode("</content>", $stepOne[1]); $tweet = $stepTwo[0]; $tweet = str_replace("<", "<", $tweet); $tweet = str_replace(">", ">", $tweet); $tweet = str_replace($username . ":", "", $tweet); return $tweet; } //Create a curl object, give it the feed and authentication $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL, $feed); curl_setopt($curl_handle,CURLOPT_USERPWD, $userpass); //Return the result, don't print it. curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER, 1); //Make the connection, set the variable, close the connection. $twitterFeed = curl_exec($curl_handle); curl_close($curl_handle); //Echo the parsed feed. Done. echo parse_feed($twitterFeed, $username); ?>
One dependency is the php-curl library, and after you install that, apache2 will want a restart.
A friend of mine pointed out that this code doesn't cache the twitter feed, so if my site gets hit hard, Twitter will likely block me out. Plus, if you don't cache, you have to pull the twitter data every load, which takes bandwidth and time.
Here's some updated code, with the caching folded in. It's a bit more complicated, but should work better:
Yeah, I get by with a little help from my friends.
Post new comment