I made this program for the Twilio+Wunderground Contest.  I picked a toll-free number with WTHR in it, and later realized the first 3 numbers spelled PIG.  Shortly thereafter, I realized the destined purpose of this phone number.

Try it here:

The code to handle the phone call is pretty simple. First, we set constants and get information from the call.

title="voice.php"
<?php
$KEY = ""; // Weather Underground API Key
$FromZip = isset($_GET['FromZip']) ? $_GET['FromZip'] : "84606";
if(isset($_GET['Digits']) && strlen($_GET['Digits'])==5) $FromZip = $_GET['Digits'];

Next, using the zip code we gathered, we get the forecast from Wunderground as json. We are going to use the current conditions, the location, and the short description of the forecast for each time period.


// Take the zip code, get the forecast
$json_string = file_get_contents("http://api.wunderground.com/api/$KEY/conditions/forecast/q/$FromZip.json");
$parsed_json = json_decode($json_string, TRUE);
$current_observation = $parsed_json['current_observation'];
$location = $current_observation['display_location'];
$txt_forecast = $parsed_json['forecast']['txt_forecast'];

We are now ready to write some TwiML using the information we received. We are going to translate everything that is spoken to Pig Latin.


// Tell Twilio what to do for the caller
print "<Response>n";
print "<Gather method='GET' numDigits='5'>n";
print "<Say voice='woman'>" . translate("Weather for " . $location['city'] . " " . $location['state_name']) . ".</Say>n";
print "<Say voice='woman'>" . translate("For a different location, enter a zip code.") . "</Say>n";
print "<Say voice='woman'>" . translate("It is currently " . $current_observation['weather'] . " at " . $current_observation['temp_f'] . " degrees fahrenheit.") . "</Say>n";
foreach($txt_forecast['forecastday'] as $day){
print "<Say voice='woman'>" . translate($day['title'] . " : " . $day['fcttext']) . "</Say>n";
}
print "</Gather>n";
print "</Response>";

The last part is the function we used to translate things to Pig Latin. It's not foolproof, but it will do.


function translate($message){
$result = "";
foreach(explode(" ", $message) as $word){
// If it's a small word, don't translate
if(strlen($word) <= 2){
$result .= "$word, ";
continue;
}
// Find the first vowel
preg_match("/^(.*?)([AEIOUaeiou]w*)W*$/", $word, $matches);
if(count($matches)>0)
$result .= $matches[2] . $matches[1] . "ay, ";
else $result .= "$word ";
}
return $result;
}
?>

Here is a screencast of what it does and how it's made:

Try it out!  Call 1-888-PIG-WTHR to hear your local conditions and forecast in Pig Latin.