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 . '
'; $planetMoneyFeed = $planetMoneyFeed . '
'; $planetMoneyFeed = $planetMoneyFeed . sprintf($img,$result->profile_image_url); $planetMoneyFeed = $planetMoneyFeed . '
'; $planetMoneyFeed = $planetMoneyFeed . '
'; $planetMoneyFeed = $planetMoneyFeed . sprintf($fromUser,$result->from_user,$result->from_user); $planetMoneyFeed = $planetMoneyFeed . $twitterText; $planetMoneyFeed = $planetMoneyFeed . ''; $planetMoneyFeed = $planetMoneyFeed . ' - at ' . $result->created_at; $planetMoneyFeed = $planetMoneyFeed . '
'; $planetMoneyFeed = $planetMoneyFeed . ''; $planetMoneyFeed = $planetMoneyFeed . '
'; } $text = str_replace('[planettwitter]',$planetMoneyFeed,$text); } return $text; } // This is here so it can be used as a Wordpress Plugin. To use it elsewhere // and just call the above function, comment this out. add_filter ( 'the_content', 'get_planet_money_chatter'); ?>