#!/usr/bin/php -q decode($json); // This array will hold the decoded "tweets" $twitters = array(); // Loop through the Object and decode the latest twitters foreach ( $obj as $twitter_obj ) { $screenname = $twitter_obj->screen_name; $name = $twitter_obj->name; $status = $twitter_obj->status; $status_text = $status->text; $status_rel_created_at = $status->relative_created_at; $status_timestamp = $status->created_at; // Twitter includes a "+0000" string in the timestamp they return that throws off PHP's strtotime function that we // use in cmp_twitters below, so we'll just strip it out. This may or may not hold up well in case twitter's format // changes down the road $status_timestamp = str_replace("+0000","",$status_timestamp); $twitters[] = array( "screenname" => $screenname, "name" => $name, "status" => $status_text, "status_rel_created_at" => $status_rel_created_at, "status_timestamp" => $status_timestamp); } /** * This is a function we use to compare the timestamp of the twitters ("tweets"). It is used by usort below * to sort the tweets into reverse chrono order so we can play back the latest ones */ function cmp_twitters( $a, $b ) { $a_ts = strtotime($a['status_timestamp']); $b_ts = strtotime($b['status_timestamp']); if ( $a_ts == $b_ts ) { return 0; } return ($a_ts < $b_ts) ? 1 : -1; } // Sort the twitters array using the function "cmp_twitters" -- puts the array into reverse chrono order usort( $twitters, "cmp_twitters"); // Now we just set the following 9 variables in Asterisk -- we'll use them to read back the tweets when this script // ends and we continue with the dialplan $agi->set_variable("TWITTER_NAME", $twitters[0]['screenname']); $agi->set_variable("TWITTER_TEXT", $twitters[0]['status']); $agi->set_variable("TWITTER_TIME", $twitters[0]['status_rel_created_at']); $agi->set_variable("TWITTER_NAME1", $twitters[1]['screenname']); $agi->set_variable("TWITTER_TEXT1", $twitters[1]['status']); $agi->set_variable("TWITTER_TIME1", $twitters[1]['status_rel_created_at']); $agi->set_variable("TWITTER_NAME2", $twitters[2]['screenname']); $agi->set_variable("TWITTER_TEXT2", $twitters[2]['status']); $agi->set_variable("TWITTER_TIME2", $twitters[2]['status_rel_created_at']); ?>