Created
April 28, 2011 04:45
-
-
Save brennanneoh/945819 to your computer and use it in GitHub Desktop.
Yahoo! Weather to SOAP
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 | |
function getTownWoeid ($location_string) { | |
$woeid=null; | |
$yqlUrl = "http://query.yahooapis.com/v1/public/yql"; | |
$query = "select woeid from geo.places where placeTypeName=\"Town\" and text=\"".$location_string."\""; | |
$queryUrl = $yqlUrl . | |
"?q=" . urlencode($query) . | |
"&format=xml"; | |
$location_feed = file_get_contents($queryUrl); | |
$location = simplexml_load_string($location_feed); | |
if(!$location) die('location failed'); | |
foreach ($location->results->children() as $place) | |
foreach($place->children() as $child) | |
$woeid = $child; | |
return $woeid; | |
} | |
function getCondition($location_string) { | |
$weather = getWeather($location_string); | |
return implode(',',$weather['condition']); | |
} | |
function getForecast($location_string) { | |
$weather = getWeather($location_string); | |
$days = count($weather['forecast']); | |
$i = 0; | |
$f_string = ""; | |
$arr = $weather['forecast']; | |
foreach ($weather['forecast'] as $day => $val) { | |
$f_string .= $day . ','; | |
$f_string .= implode(',',$val) . ';'; | |
} | |
return $f_string; | |
} | |
function getWeather($location_string) { | |
$woeid=getTownWoeid($location_string); | |
$weatherApi = "http://weather.yahooapis.com/forecastrss"; | |
$weatherUrl = $weatherApi . | |
"?w=" . $woeid . | |
"&u=c"; | |
$weather_feed = file_get_contents($weatherUrl); | |
$weather = simplexml_load_string($weather_feed); | |
if(!$weather) die('weather failed'); | |
$copyright = $weather->channel->copyright; | |
$channel_yweather = $weather->channel->children("http://xml.weather.yahoo.com/ns/rss/1.0"); | |
foreach($channel_yweather as $x => $channel_item) | |
foreach($channel_item->attributes() as $k => $attr) | |
$yw_channel[$x][$k] = $attr; | |
$item_yweather = $weather->channel->item->children("http://xml.weather.yahoo.com/ns/rss/1.0"); | |
foreach($item_yweather as $x => $yw_item) { | |
foreach($yw_item->attributes() as $k => $attr) { | |
if($k == 'day') $day = $attr; | |
if($x == 'forecast') { $yw_forecast[$x][$day . ''][$k] = $attr; } | |
else { $yw_forecast[$x][$k] = $attr; } | |
} | |
} | |
return $yw_forecast; | |
} | |
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache | |
$server = new SoapServer("weather.wsdl"); | |
$server->addFunction("getCondition"); | |
$server->addFunction("getForecast"); | |
$server->handle(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A PHP SOAP server I created for my SMU module, Enterprise Integration.