From 4b5e93f27523e53cc3b4ef1dd72abd57eaac7e36 Mon Sep 17 00:00:00 2001 From: Harika Nukala Date: Mon, 12 Dec 2016 19:28:40 -0800 Subject: [PATCH] added weather bot in examples/bot_examples --- src/examples/bot_examples/weather/index.php | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/examples/bot_examples/weather/index.php diff --git a/src/examples/bot_examples/weather/index.php b/src/examples/bot_examples/weather/index.php new file mode 100644 index 0000000..14a7afb --- /dev/null +++ b/src/examples/bot_examples/weather/index.php @@ -0,0 +1,91 @@ +access_token = $access_token; + } + /** + * Get weather information + * + * @param string $location the location to get weather updates for + * @return string weather information + */ + function getWeather($location) + { + $BASE_URL = "http://query.yahooapis.com/v1/public/yql"; + $yql_query = "select * from weather.forecast where woeid in + (select woeid from geo.places(1) where text='".$location."')"; + $Url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json"; + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $Url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $data = curl_exec($ch); + curl_close($ch); + $result = json_decode($data); + $temp = $result->query->results->channel->item->condition->temp; + $text = $result->query->results->channel->item->condition->text; + return "The weather is " . $temp . " and " . $text . " in " . $location; + } +} + +$access_token ="bot_token"; +$bot = new WeatherBot(); +$bot->setKey($access_token); +$location = ""; +$result = ""; +if(!empty($_POST['post'])){ + $location = explode(" ", $_POST['post']); +} +if(!empty($location[4])) { + $result = $bot->getWeather($location[4]); +} +echo $result; -- 2.9.3 (Apple Git-75)