$apiKeyParam = $_GET["apiKey"]; $latParam = $_GET["lat"]; $lonParam = $_GET["lon"]; $unitsParam = $_GET["units"]; $langParam = $_GET["lang"]; $units = 'us'; // Can be set to 'us', 'si', 'ca', 'uk' or 'auto' (see forecast.io API); default is auto $lang = 'en'; // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' if ($unitsParam != "") { $units = $unitsParam; } if ($langParam != "") { $lang = $langParam; } //error_log(date(DATE_RFC822)." -- api=".$apiKeyParam.",lat=".$latParam.",lon=".$lonParam.",units=".$units.",lang=".$lang."\n", 3, '/home/weather.log'); $forecast = new ForecastIO($apiKeyParam, $units, $lang); $condition = $forecast->getCurrentConditions($latParam, $lonParam); echo "CURRENT_TEMP=" . round($condition->getTemperature()) . "\n"; echo "CURRENT_HUMIDITY=" . $condition->getHumidity() * 100 . "\n"; echo "CURRENT_ICON=" . $condition->getIcon() . "\n"; echo "CURRENT_SUMMARY=" . $condition->getSummary() . "\n"; $conditions_week = $forecast->getForecastWeek($latParam, $lonParam); echo "MAX_TEMP_TODAY=" . round($conditions_week[0]->getMaxTemperature()) . "\n"; echo "MIN_TEMP_TODAY=" . round($conditions_week[0]->getMinTemperature()) . "\n"; echo "ICON_TODAY=" . $conditions_week[0]->getIcon() . "\n"; echo "SUMMARY_TODAY=" . $conditions_week[0]->getSummary() . "\n"; echo "MAX_TEMP_TOMORROW=" . round($conditions_week[1]->getMaxTemperature()) . "\n"; echo "ICON_TOMORROW=" . $conditions_week[1]->getIcon() . "\n"; echo "MIN_TEMP_TOMORROW=" . round($conditions_week[1]->getMinTemperature()) . "\n"; echo "SUMMARY_TODAY=" . $conditions_week[1]->getSummary() . "\n";
$lang = 'en'; // Can be set to 'en', 'de', 'pl', 'es', 'fr', 'it', 'tet' or 'x-pig-latin' (see forecast.io API); default is 'en' $forecast = new ForecastIO($api_key, $units, $lang); // all default will be // $forecast = new ForecastIO($api_key); /* * GET CURRENT CONDITIONS */ $condition = $forecast->getCurrentConditions($latitude, $longitude); echo 'Current temperature: ' . $condition->getTemperature() . "\n"; /* * GET HOURLY CONDITIONS FOR TODAY */ $conditions_today = $forecast->getForecastToday($latitude, $longitude); echo "\n\nTodays temperature:\n"; foreach ($conditions_today as $cond) { echo $cond->getTime('H:i:s') . ': ' . $cond->getTemperature() . "\n"; } /* * GET DAILY CONDITIONS FOR NEXT 7 DAYS */ $conditions_week = $forecast->getForecastWeek($latitude, $longitude); echo "\n\nConditions this week:\n"; foreach ($conditions_week as $conditions) { echo $conditions->getTime('Y-m-d') . ': ' . $conditions->getMaxTemperature() . "\n"; } /* * GET HISTORICAL CONDITIONS */ $condition = $forecast->getHistoricalConditions($latitude, $longitude, '2010-10-10T14:00:00-0700'); echo "\n\nTemperatur 2010-10-10: " . $condition->getMaxTemperature() . "\n";
function getweather($api_key, $latitude, $longitude, $units = "auto", $lang = "en") { $forecast = new ForecastIO($api_key, $units, $lang); //This is array holding CURRENT weather data i.e. weather right now $now = array(); $yesterday = array(); //This holds today's, tomorrow's and the day after's weather data $twoday = array(); //create a timestamp of yestday, so we can retrieve its weather $today = new DateTime("now"); $yesterdaytime = $today->sub(new DateInterval('P1D'))->getTimeStamp(); //echo $yesterday; //Get current conditions and give to array too $condition = $forecast->getCurrentConditions($latitude, $longitude); echo "current: " . $condition->getTemperature() . "\n"; echo "percent rain: " . $condition->getPrecipitationProbability() . "\n"; echo "humidity: " . $condition->getHumidity() . "\n"; echo "rain fall: " . $condition->getPrecipitationIntensity(); $now['temp'] = $condition->getTemperature(); $now['chancerain'] = $condition->getPrecipitationProbability(); $now['humidity'] = $condition->getHumidity(); $now['summary'] = $condition->getSummary(); $now['rainfall'] = $condition->getPrecipitationIntensity(); //If precip probability > 0 then get precip type too if ($now['chancerain'] > 0) { echo "Precip type: " . $current->PrecipitationType() . "\n"; $now['preciptype'] = $current->PrecipitationType(); } //Get weekly forecast (next 2 days is fine) $conditions_week = $forecast->getForecastWeek($latitude, $longitude); //print_r($conditions_week); echo "\nConditions this week:\n"; $i = 0; foreach ($conditions_week as $conditions) { $cheese = new DateTime("now"); $step = new DateInterval('P2D'); //If day is less then or equal to 2 days from now then we want that info if ($cheese->add($step)->format('Y-m-d') >= $conditions->getTime('Y-m-d')) { echo $conditions->getTime('Y-m-d') . ": " . $conditions->getMaxTemperature() . "\n"; $twoday[$i]['maxtemp'] = $conditions->getMaxTemperature(); $twoday[$i]['chancerain'] = $conditions->getPrecipitationProbability(); $twoday[$i]['rainfall'] = $conditions->getPrecipitationIntensity(); $twoday[$i]['rainfallmax'] = $conditions->getPrecipitationIntensityMax(); if ($twoday[$i]['rainfallmax'] > 0) { $twoday[$i]['rainfallmaxtime'] = $conditions->getPrecipitationIntensityMaxTime(); } if ($twoday[$i]['chancerain'] > 0) { echo "Precip type: " . $conditions->getPrecipitationType() . "\n"; $twoday[$i]['preciptype'] = $conditions->getPrecipitationType(); } $twoday[$i]['humidity'] = $conditions->getHumidity(); $twoday[$i]['summary'] = $conditions->getSummary(); } $i++; } //Get Yesterdays forecast echo "\n Yesterdays conditions: \n"; $yesterdays = $forecast->getHistoricalConditions($latitude, $longitude, $yesterdaytime); echo "Yesterday: " . $yesterdays->getMaxTemperature() . "\n"; echo "percent rain: " . $yesterdays->getPrecipitationProbability() . "\n"; echo "humidity: " . $yesterdays->getHumidity() . "\n"; echo "rain fall: " . $yesterdays->getPrecipitationIntensity(); $yesterday['temp'] = $yesterdays->getMaxTemperature(); $yesterday['chancerain'] = $yesterdays->getPrecipitationProbability(); $yesterday['humidity'] = $yesterdays->getHumidity(); $yesterday['summary'] = $yesterdays->getSummary(); $yesterday['rainfall'] = $yesterdays->getPrecipitationIntensity(); //If precip probability > 0 then get precip type too if ($yesterday['chancerain'] > 0) { echo "Precip type: " . $yesterdays->PrecipitationType() . "\n"; $yesterdays['preciptype'] = $yesterdays->PrecipitationType(); } //print_r($yesterdays); //return array with current, today, tomorrow, and day after temp/conditions return [$now, $twoday]; }