'; } else { // 1.2. search term: display all alike places // $json_string = curl_download("searchResult.txt"); // for offline testing only $json_string = curl_download("http://autocomplete.wunderground.com/aq?query=".$placeUserisLookingFor); $places_array = json_decode($json_string); $places_array = $places_array->RESULTS; $placesFound = ""; for ($i = 0; $i < count($places_array); $i++) { $placesFound .= ''; } echo '
'.$placesFound.'
'; if(empty($placesFound)) { echo "

Nothing found, sorry. Try another search term. Maybe wunderground has no weather data on this place.

"; echo "

Please use english Terms! Thanks!

"; } } // 2. has user selected a place if(isset($_REQUEST["placeFound"])) { $placeFound = $_REQUEST["placeFound"]; } // 2.1. no place selected -> do nothing // 2.2. user has selected a place: display 3 day weather forecast of that place if(empty($placeFound)) { if(empty($placesFound)) { $placeFound = "00000.1.10838"; // default is ulm } else { $placeFound = $places_array[0]->zmw; // use the first hit in the searchlist } } $json_string = curl_download("http://api.wunderground.com/api/YOUR-API-KEY-HERE/forecast/conditions/alerts/lang:US/q/zmw:".$placeFound.".json"); // $json_string = curl_download("json.txt"); // for offline testing only $parsed_json = json_decode($json_string); if(isset($parsed_json)) { if(!empty($parsed_json)) { if(isset($parsed_json->current_observation)) { if(!empty($parsed_json->current_observation)) { // current conditions $location = $parsed_json->{'current_observation'}->{'display_location'}->{'full'}; $temp_c = $parsed_json->{'current_observation'}->{'temp_c'}; $relative_humidity = $parsed_json->{'current_observation'}->{'relative_humidity'}; $wind_direction = $parsed_json->{'current_observation'}->{'wind_dir'}; $weather = $parsed_json->{'current_observation'}->{'weather'}; $wind_speed = $parsed_json->{'current_observation'}->{'wind_kph'}."km/h"; // forecast $forecast = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}; $simpleforecast = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}; $Today = printDay(getDay(0),"Prediction Today:"); $Tomorrow = printDay(getDay(1),"Prediction Tomorrow:"); $DayAfterTomorrow = printDay(getDay(2),"Prediction the Day after Tomorrow:"); echo "

$location

"; echo "

Current: $weather
Temp: $temp_c\n °C
Wind from: $wind_direction\n with speed: ".$wind_speed."

"; echo "

$Today

"; echo "

$Tomorrow

"; echo "

$DayAfterTomorrow

"; } else { echo 'I am sorry, we are out of wunderground-tickets! (the free service only allows a certain amount of requests per day. http://www.wunderground.com/weather/api/d/terms.html'; } } } } /* DayNumber is 0 = today, 1 = tomorrow, 2 = day after tomorrow... max 6 */ function getDay($DayNumber) { global $forecast, $simpleforecast; $result = null; $day = $forecast[$DayNumber]; $result['dayofweek'] = $day->{'title'}; $result['text'] = $day->{'fcttext_metric'}; $day_array = explode('.',$result['text']); $result['forecast_weather'] = $simpleforecast[$DayNumber]->{'conditions'}; // "conditions": "Teils Wolkig", $result['icon'] = $simpleforecast[$DayNumber]->{'icon_url'}; // "icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", $result['forecast_high'] = "MaxTemp: ".$simpleforecast[$DayNumber]->{'high'}->{'celsius'}."°C"; $result['forecast_low'] = "LowTemp: ".$simpleforecast[$DayNumber]->{'low'}->{'celsius'}."°C"; $result['forecast_humidity'] = "Humidity: ".$simpleforecast[$DayNumber]->{'avehumidity'}."%"; $result['forecast_wind'] = "WindSpeed of: ".$simpleforecast[$DayNumber]->{'avewind'}->{'kph'}."km/h"; $result['forecast_winddirection'] = $simpleforecast[$DayNumber]->{'avewind'}->{'dir'}; // Südwest $result['forecast_rainrisk'] = "Probability of Rain: ".$simpleforecast[$DayNumber]->{'pop'}."%"; // probability of precipitation = regenwahrscheinlichkeit return $result; } function printDay($day,$title) { return "

$title ".$day['forecast_weather']."
".$day['forecast_high']."
".$day['forecast_low']."
".$day['forecast_rainrisk']."
Wind from ".$day['forecast_winddirection']." with ".$day['forecast_wind']."

"; } /* because file_get_contents(); and fopen are disabled on some servers use CURL */ function curl_download($Url){ // is cURL installed yet? if (!function_exists('curl_init')){ die('Sorry cURL is not installed!'); } // OK cool - then let's create a new cURL resource handle $ch = curl_init(); // Now set some options (most are optional) // Set URL to download curl_setopt($ch, CURLOPT_URL, $Url); // Set a referer // curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm"); // if server needs to think this post came from elsewhere // User agent curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); // Include header in result? (0 = yes, 1 = no) curl_setopt($ch, CURLOPT_HEADER, 0); // Should cURL return or print out the data? (true = return, false = print) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Download the given URL, and return output $output = curl_exec($ch); // Close the cURL resource, and free system resources curl_close($ch); return $output; } ?>