PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/get_zip_code.php

http://github.com/mheadd/tropo-webapi-php
PHP | 142 lines | 68 code | 36 blank | 38 comment | 7 complexity | 962d735d12418bac6eea3b68123d52e0 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. // Include Tropo classes.
  3. require('tropo.class.php');
  4. // Include Limonade framework.
  5. require('path/to/limonade/lib/limonade.php');
  6. // The URL to the Google weather service. Renders as XML doc.
  7. define("GOOGLE_WEATHER_URL", "http://www.google.com/ig/api?weather=%zip%&hl=en");
  8. // A helper method to get weather details by zip code.
  9. function getWeather($zip) {
  10. $url = str_replace("%zip", $zip, GOOGLE_WEATHER_URL);
  11. $weatherXML = simplexml_load_file($url);
  12. $city = $weatherXML->weather->forecast_information->city["data"];
  13. $current_conditions = $weatherXML->weather->current_conditions;
  14. $current_weather = array(
  15. "condition" => $current_conditions->condition["data"],
  16. "temperature" => $current_conditions->temp_f["data"]." degrees",
  17. "wind" => formatDirection($current_conditions->wind_condition["data"]),
  18. "city" => $city
  19. );
  20. return $current_weather;
  21. }
  22. // A helper method to format directional abbreviations.
  23. function formatDirection($wind) {
  24. $abbreviated = array(" N ", " S ", " E ", " W ", " NE ", " SE ", " SW ", " NW ");
  25. $full_name = array(" North ", " South ", " East ", " West ", " North East ", " South East ", " South West ", " North West ");
  26. return str_replace($abbreviated, $full_name, str_replace("mph", "miles per hour", $wind));
  27. }
  28. // A helper method to format the Tropo object with weather details for a specific zip code.
  29. function formatWeatherResponse(&$tropo, $zip) {
  30. // Get weather information for the zip code the caller entered.
  31. $weather_info = getWeather($zip);
  32. $city = array_pop($weather_info);
  33. // Begin telling the user the weather for the city their zip code is in.
  34. $tropo->say("The current weather for $city is...");
  35. // Iterate over an array of weather information.
  36. foreach ($weather_info as $info) {
  37. $tropo->say("$info.");
  38. }
  39. // Say thank you (never hurts to be polite) and end the session.
  40. $tropo->say("Thank you for using Tropo!");
  41. $tropo->hangup();
  42. }
  43. /**
  44. * This is the starting point for the Tropo application.
  45. * Get a 5 digit zip code from the user.
  46. */
  47. dispatch_post('/start', 'zip_start');
  48. function zip_start() {
  49. // Create a new instance of the Session object, and get the channel information.
  50. $session = new Session();
  51. $from_info = $session->getFrom();
  52. $channel = $from_info['channel'];
  53. // Create a new instance of the Tropo object.
  54. $tropo = new Tropo();
  55. // See if any text was sent with session start.
  56. $initial_text = $session->getInitialText();
  57. // If the initial text is a zip code, skip the input collection and get weather information.
  58. if(strlen($initial_text) == 5 && is_numeric($initial_text)) {
  59. formatWeatherResponse($tropo, $initial_text);
  60. }
  61. else {
  62. // Welcome prompt (for phone channel, and IM/SMS sessions with invalid initial text).
  63. $tropo->say("Welcome to the Tropo PHP zip code example for $channel");
  64. // Set up options form zip code input
  65. $options = array("attempts" => 3, "bargein" => true, "choices" => "[5 DIGITS]", "name" => "zip", "timeout" => 5);
  66. // Ask the user for input, pass in options.
  67. $tropo->ask("Please enter your 5 digit zip code.", $options);
  68. // Tell Tropo what to do when the user has entered input, or if there is an error.
  69. $tropo->on(array("event" => "continue", "next" => "get_zip_code.php?uri=end", "say" => "Please hold."));
  70. $tropo->on(array("event" => "error", "next" => "get_zip_code.php?uri=error", "say" => "An error has occured."));
  71. }
  72. // Render the JSON for the Tropo WebAPI to consume.
  73. return $tropo->RenderJson();
  74. }
  75. /**
  76. * After a zip code has been entered, use it to look up weather details for that city.
  77. */
  78. dispatch_post('/end', 'zip_end');
  79. function zip_end() {
  80. // Create a new instance of the result object and get the value of the user input.
  81. $result = new Result();
  82. $zip = $result->getValue();
  83. // Create a new instance of the Tropo object.
  84. $tropo = new Tropo();
  85. // Get the weather information for the entered zip code.
  86. formatWeatherResponse($tropo, $zip);
  87. // Render the JSON for the Tropo WebAPI to consume.
  88. return $tropo->RenderJson();
  89. }
  90. /**
  91. * If an error occurs, end the session.
  92. */
  93. dispatch_post('/error', 'zip_error');
  94. function zip_error() {
  95. // Step 1. Create a new instance of the Tropo object.
  96. $tropo = new Tropo();
  97. // Step 2. This is the last thing the user will be told before the session ends.
  98. $tropo->say("Please try your request again later.");
  99. // Step 3. End the session.
  100. $tropo->hangup();
  101. // Step 4. Render the JSON for the Tropo WebAPI to consume.
  102. return $tropo->renderJSON();
  103. }
  104. // Run this sucker!
  105. run();
  106. ?>