Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hoiio Example: Weather by Phone
<?php
/*
Check weather by phone
APIs used
- ivr/middle/gather
- ivr/middle/play
API Flow
1. <-- Incoming call to Hoiio number
2. --> IVR Gather API: gets ZIP code from caller
3. <-- ZIP code from from caller
4. --> IVR Play API: Read out the forecast.
5. <-- Done notification after reading. Repeat from step 2.
In this example, we are going to describe how to build an application that reads out the
weather forecast. The caller calls into a number, enters a zip code, and hears the
forecast. The application loops until the user hangs up the phone.
This example uses Yahoo weather API (http://developer.yahoo.com/weather/)
*/
// Developer information: appId, accessToken
$appId = "YOUR_APP_ID_HERE";
$accessToken = "YOUR_ACCESS_TOKEN_HERE";
$server = "https://secure.hoiio.com/open";
$myUrl = "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF'];
if ($_POST == null) {
return;
}
// New incoming calls
if ($_POST['call_state'] == "ringing") {
askZip();
return;
}
else if ($_POST['call_state'] == "ongoing") {
switch ($_POST['tag']) {
case "askZip":
checkDigitPressed();
break;
case "playMessage":
askZip();
break;
}
}
else if ($_POST['call_state'] == "ended") {
// Call has ended
}
return;
function askZip() {
// Send a Gather API to confirm the subscription
$data = http_build_query(
array( "app_id" => $GLOBALS['appId'],
"access_token" => $GLOBALS['accessToken'],
"tag" => "askZip",
"session" => $_POST['session'],
"msg" => "Please enter your W-O-E-I-D code.",
"max_digits" => "9",
"notify_url" => $GLOBALS['myUrl']));
$response = do_post_request($GLOBALS['server']."/ivr/middle/gather", $data);
$result = json_decode($response); // can do the checking here with the result
}
function checkDigitPressed() {
// Retrieve the digits pressed by the user.
$zip = $_POST['digits'];
// Checks the zip code
if (strpos($zip, '*') !== false) {
playMessage("You have entered an invalid W-O-E-I-D code");
}
else {
// Get the forecast
$forecastStr = do_get_request("http://weather.yahooapis.com/forecastrss?w=" . $zip . "&u=c");
echo "http://weather.yahooapis.com/forecastrss?w=" . $zip . "&u=c";
print_r($forecastStr);
if (strpos($forecastStr, "City not found")) {
playMessage("You have entered an invalid W-O-E-I-D code");
} else {
// Extract the temperature info
$rss = new SimpleXMLElement($forecastStr);
$temp = $rss->channel->item->children('yweather', true)->condition->attributes()->temp;
$condition = $rss->channel->item->children('yweather', true)->condition->attributes()->text;
$location = $rss->channel->children('yweather', true)->location->attributes()->city;
playMessage("It is currently ". $temp ." degrees celsius and ". $condition ." in ". $location .".");
}
}
}
function playMessage($msg) {
// Play a message
$data = http_build_query(
array( "app_id" => $GLOBALS['appId'],
"access_token" => $GLOBALS['accessToken'],
"tag" => "playMessage",
"session" => $_POST['session'],
"msg" => $msg,
"notify_url" => $GLOBALS['myUrl']));
$response = do_post_request($GLOBALS['server']."/ivr/middle/play", $data);
$result = json_decode($response);
}
// Source adapted from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
// This function will do the http post to the server, feel free to use different function of your desire
function do_post_request($url, $data) {
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
function do_get_request($url) {
$params = array('http' => array(
'method' => 'GET'
));
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment