Privatizing the Twitter API Feed

Tagged:  

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("&lt;", "<", $tweet);
  $tweet = str_replace("&gt;", ">", $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:

<?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";
&nbsp;
//Some variables for the cache
$cache_lifetime = 300; // Seconds to for cache to live
$cache_file = '/tmp/twitter'; // Path to cache file
 
//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("&lt;", "<", $tweet);
  $tweet = str_replace("&gt;", ">", $tweet);
  $tweet = str_replace($username . ":", "", $tweet); 
  return $tweet;
}
 
//Check for the cache. If it exists, use it. If not, create it. Either way, echo the feed.
if (file_exists($cache_file) && (filemtime($cache_file) + $cache_lifetime >= time()) ) { 
  // Cache good -> load
  echo unserialize(file_get_contents($cache_file));
} 
else {
  // Cache expired/disabled/missing -> fetch
 
  //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);
 
  //Save the result
  file_put_contents($cache_file, serialize(parse_feed($twitterFeed, $username)));
 
  //And echo the result
  echo unserialize(file_get_contents($cache_file));
} 
 
?>

Yeah, I get by with a little help from my friends.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.