60)
{
// Grab a JSON file of the top tweets containing 'planetmoney'
// and save it to the cache file.
$ch = curl_init('http://search.twitter.com/search.json?q=planetmoney');
$fp = fopen($cacheFile, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
// Open up the cache file
$fp = @fopen($cacheFile,"r");
$buffer = "";
// As long as the cache file exists, stick it in the $buffer
if(!$fp) {
return false;
}
else {
while(!feof($fp)) {
$buffer .= fgets($fp,4096);
}
}
fclose($fp);
// Let JSON.php decode the JSON into an object so we can parse it.
$searchResults = json_decode($buffer);
// Set up our string format templates
$img = '';
$bold = '%s';
$fromUser = '%s ';
$planetMoneyFeed = '';
// For each response we found, parse it into something pretty
foreach ($searchResults->results as $result)
{
// Replace any @replies or links with hrefs so they link to
// the right places.
$twitterText = $result->text;
$twitterText = preg_replace("#(^|[\n ])@([^ \'\"\t\n\r<]*)#ise", "'\\1@\\2'", $twitterText);
$twitterText = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t<]*)#ise", "'\\1\\2'", $twitterText);
$twitterText = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r<]*)#ise", "'\\1\\2'", $twitterText);
// This gets the profile image, the from user and the created date
// and sticks it into HTML to make it look attractive.
$planetMoneyFeed = $planetMoneyFeed . '