Hoiio API Example: WeatherByPhone

February 17, 2012

Another simple yet interesting example was developed using Hoiio API.

This example let you check the weather of your location by using your mobile phone. It makes use of Yahoo weather API.

Application flow:

The app requires the WOEID code which is introduced by Yahoo to uniquely identify any location on Earth. To find the WOEID of your location, you can find by browser/search from the Yahoo weather homepage. The WOEID is in the URL for the forecast page for that city. For example, if you search for Singapore on the Weather home page, the forecast page for that city is http://weather.yahoo.com/singapore/central-singapore/singapore-1062617/. The WOEID is 1062617.

The response user will hear is “It is currently 28 degrees celsius and partly cloudy in Singapore“. The application will keep asking the user until he/she hangs up the phone.

Let’s look closer into the code:

  1. Global variables: we define all global variables like appId, accessToken,…
    $appId       = "[Your appId]";
    $accessToken = "[Your accessToken]";
    $server      = "https://secure.hoiio.com/open";
    $myUrl       = "[Your notify URL address]";
    
    1. The appId and accessToken are generated for you when you create an application in the developer portal
    2. The notify URL is the address that Hoiio will notify developer after each step to ask for the next step (or next building block)
  2. The controller: maintain the state of the call to do correct actions
    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, can do anything here
    }
  3. The askZip() function: send a Gather API to ask for the WOEID codes
    $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']));
    
    do_post_request($GLOBALS['server']."/ivr/middle/gather", $data);
    
  4. The checkDigitPressed() function:
    $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");
    
    	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 .".");
    	}
    }
    

    This function verifies the code then send the http request to Yahoo to get the weather information of that location. If the response is “City not found”, it asks again for the WOEID code.

  5. The playMessage() function: play the result to the user
    $data = http_build_query(
    	array(	"app_id" 		=> $GLOBALS['appId'],
    			"access_token" 	=> $GLOBALS['accessToken'],
    			"tag"			=> "playMessage",
    			"session"		=> $_POST['session'],
    			"msg"			=> $msg,
    			"notify_url" 	=> $GLOBALS['myUrl']));
    
    do_post_request($GLOBALS['server']."/ivr/middle/play", $data);
    

That’s it! It’s simple and straight forward and you can do it in less than 30 minutes.

Download

The source code you can download here.

Be Sociable, Share!
Tags: , ,
Posted by: