Last active
August 29, 2015 14:23
-
-
Save juliandunn/56684232311b0fd38c55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$weather_api_url = "http://weather.yahooapis.com/forecastrss?p="; | |
$weather_api_ns = "http://xml.weather.yahoo.com/ns/rss/1.0"; | |
function handle_digits($digits) { | |
$cleandigits = str_replace(array("#", "*"), "", $digits); | |
$maxdigits = substr($cleandigits, 0, 5); | |
$xml = fetch_data_for_zip($maxdigits); | |
list($city, $region, $text, $temp) = parse_xml($xml); | |
$message = "It is currently $temp degrees Fahrenheit and $text in $city, $region"; | |
saymessage($message); | |
} | |
function parse_xml($xml) { | |
$dom = new DOMDocument(); | |
$dom->loadXML($xml); | |
$conditions = $dom->getElementsbyTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0", "condition")->item(0); | |
$location = $dom->getElementsbyTagNameNS("http://xml.weather.yahoo.com/ns/rss/1.0", "location")->item(0); | |
return array($location->getAttribute("city"), | |
$location->getAttribute("region"), | |
$conditions->getAttribute("text"), | |
$conditions->getAttribute("temp")); | |
} | |
function fetch_data_for_zip($zip) { | |
$url = "http://weather.yahooapis.com/forecastrss?p=$zip"; | |
$options = array( | |
'http' => array( | |
'method' => 'GET', | |
'content' => $url, | |
'header' => "Content-Type: application/xml\r\n" | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
return $result; | |
} | |
function saymessage($msg = "An error occurred", $redirect = NULL) { | |
header('Content-Type: application/xml'); | |
$xmlDoc = new DOMDocument("1.0", "UTF-8"); | |
$response = $xmlDoc->appendChild($xmlDoc->createElement("Response")); | |
$response->appendChild($xmlDoc->createElement("Say", $msg)); | |
if ($redirect) { | |
$response->appendChild($xmlDoc->createElement("Redirect", $redirect)); | |
} | |
$xmlDoc->formatOutput = true; | |
echo $xmlDoc->saveXML(); | |
} | |
function saygreeting() { | |
header('Content-Type: application/xml'); | |
$self = $_SERVER['PHP_SELF']; | |
$xmlDoc = new DOMDocument("1.0", "UTF-8"); | |
$response = $xmlDoc->appendChild($xmlDoc->createElement("Response")); | |
$gather = $response->appendChild($xmlDoc->createElement("Gather")); | |
$gather->appendChild($xmlDoc->createAttribute("method"))->appendChild($xmlDoc->createTextNode("POST")); | |
$gather->appendChild($xmlDoc->createAttribute("numDigits"))->appendChild($xmlDoc->createTextNode("5")); | |
$gather->appendChild($xmlDoc->createAttribute("action"))->appendChild($xmlDoc->createTextNode($self)); | |
$gather->appendChild($xmlDoc->createElement("Play", "greeting.wav")); | |
$gather->appendChild($xmlDoc->createElement("Say", "Please enter your 5 digit zipcode to hear the weather.")); | |
$xmlDoc->formatOutput = true; | |
echo $xmlDoc->saveXML(); | |
} | |
if (isset($_REQUEST['Digits'])) { | |
handle_digits($_REQUEST['Digits']); | |
} else { | |
saygreeting(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment