function getCityWeather($cities, $zipCodes)
{
    global $allLatLong, $navbarLinks;
    $BASE_URL = "http://query.yahooapis.com/v1/public/yql";
    $yql_query = 'select * from weather.forecast where location in (' . $zipCodes . ')';
    $yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
    // Make call with cURL
    $session = curl_init($yql_query_url);
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    $json = curl_exec($session);
    // Convert JSON to PHP object
    $weatherObj = json_decode($json);
    $count = $weatherObj->query->count;
    $html = '';
    //for loop iterates through all selected cities returned by Yahoo
    for ($x = 0; $x < $count; $x++) {
        if ($count > 1) {
            //if more than 1 city is selected, the array for the city must be specified
            $channel = $weatherObj->query->results->channel[$x];
        } else {
            $channel = $weatherObj->query->results->channel;
        }
        if ($x == 0) {
            $unitsOfDegree = $channel->units->temperature;
            $unitsOfSpeed = $channel->units->speed;
            $unitsOfPressure = $channel->units->pressure;
            $unitsOfDistance = $channel->units->distance;
            $display = '';
            $active = 'active';
        } else {
            $display = 'displayNone';
            $active = '';
        }
        $buildDateTime = $channel->lastBuildDate;
        $cityName = $channel->location->city;
        $state = $channel->location->region;
        $zipCode = $cities[$x];
        $windDir = degToCompass($channel->wind->direction);
        $windSpeed = $channel->wind->speed;
        $humidity = $channel->atmosphere->humidity;
        $pressure = $channel->atmosphere->pressure;
        $visibility = $channel->atmosphere->visibility;
        $sunrise = $channel->astronomy->sunrise;
        $sunset = $channel->astronomy->sunset;
        $temp = $channel->item->condition->temp;
        $conditionType = $channel->item->condition->text;
        $latitude = $channel->item->lat;
        $longitude = $channel->item->long;
        $navbarLinks .= '<li class="' . $active . '"><a href="#" view="' . $zipCode . '" class="navbarSelected ">' . $cityName . ', ' . $state . '</a></li>';
        $latLong = array("latitude" => $latitude, "longitude" => $longitude);
        $allLatLong[$x] = $latLong;
        $html .= '<div class="col-xs-12 col-sm-12 col-md-12 cityBody ' . $display . ' active" id="' . $zipCode . '"><div class="row"><div class="col-xs-12 col-sm-8 col-md-8 cityForecast"><div class="row"><div class="col-xs-12 col-sm-12 col-md-12 forecastHeader "><h3 class="well well-lg">Weather for: ' . $cityName . ', ' . $state . ' (' . $zipCode . ')<br><span class="build">As of ' . $buildDateTime . '</span></h3></div><div class="col-xs-12 col-sm-6 col-md-6"><div><h5>Currently:</h5><p><span class="temperature">' . $temp . '&deg; ' . $unitsOfDegree . '</span><br><span class="condition">' . $conditionType . '</span></p></div><table class="sunRiseSet"><tr class="label-warning"><th>Sunrise:</th><th>' . $sunrise . '</th></tr><tr class="label-primary"><th>Sunset:</th><th>' . $sunset . '</th></tr></table></div><div class="col-xs-12 col-sm-6 col-md-6"><table class="otherConditions table table-hover"><tr><th>Wind:</th><td>' . $windDir . ' ' . $windSpeed . ' ' . $unitsOfSpeed . '</td></tr><tr><th>Humidity:</th><td>' . $humidity . '</td></tr><tr><th>Pressure:</th><td>' . $pressure . ' ' . $unitsOfPressure . '</td></tr><tr><th>Visibility:</th><td>' . $visibility . ' ' . $unitsOfDistance . '</td></tr></table></div><div class="col-xs-12 col-sm-12 col-md-12 "><div class="dailyForecast"><h4>5 Day Forecast</h4><table class="table table-striped table-condensed"><tr><th>Day</th><th>Conditions</th><th>High/Low</th></tr>';
        for ($y = 0; $y < 5; $y++) {
            $day = $channel->item->forecast[$y]->day;
            $date = $channel->item->forecast[$y]->date;
            $dates = explode(" ", $date);
            $dateNum = $dates[0];
            $dateMon = $dates[1];
            $high = $channel->item->forecast[$y]->high;
            $low = $channel->item->forecast[$y]->low;
            $dailyCondition = $channel->item->forecast[$y]->text;
            if ($dailyCondition == 'Sunny') {
                $dailyCondition = '<span class="glyphicon glyphicon-sunglasses yellow"></span> ' . $dailyCondition;
            }
            if ($dailyCondition == 'Mostly Sunny') {
                $dailyCondition = '<span class="glyphicon glyphicon-sunglasses blue"></span> ' . $dailyCondition;
            } elseif ($dailyCondition == 'Rain' || $dailyCondition == 'Rainy' || $dailyCondition == 'Thunderstorms') {
                $dailyCondition = '<span class="glyphicon glyphicon-tint"></span> ' . $dailyCondition;
            } elseif ($dailyCondition == 'Mostly Cloudy') {
                $dailyCondition = '<span class="glyphicon glyphicon-cloud darkGrey"></span> ' . $dailyCondition;
            } elseif ($dailyCondition == 'Cloudy' || $dailyCondition == 'Partly Cloudy') {
                $dailyCondition = '<span class="glyphicon glyphicon-cloud grey"></span> ' . $dailyCondition;
            }
            $html .= '<tr><td>' . $day . '<br>' . $dateMon . ' ' . $dateNum . '</td><td>' . $dailyCondition . '</td><td>' . $high . '&deg;/' . $low . '&deg;</td></tr>';
        }
        $html .= '</table></div></div></div></div><div class="col-xs-12 col-sm-4 col-md-4" ><span class="visible-sm-block visible-md-block visible-lg-block locationMap" id="map_' . $x . '"></span></div></div></div>';
    }
    return $html;
}
Exemple #2
0
$data = json_decode($json, true);
$sunrise = date("H:i:s", $data["sys"]["sunrise"] - 3600);
$sunset = date("H:i:s", $data["sys"]["sunset"] - 3600);
$currenttemp = $data["main"]["temp"] - 272.15;
$maxtemp = $data["main"]["temp_max"] - 272.15;
$mintemp = $data["main"]["temp_min"] - 272.15;
$humidity = $data["main"]["humidity"];
$pressure = $data["main"]["pressure"];
$windspeed = $data["wind"]["speed"] * 3.6;
// comes in m/s, wank in km/hr
$rain = null;
if (isset($data["rain"]["1h"])) {
    $rain = $data["rain"]["1h"];
}
echo "<h2> Weather for " . $data["name"] . " as of " . date("H:i:s", $data["dt"] - 3600) . "</h2>";
echo "<div class='weather'>Sunrise is " . $sunrise . "<br>\nSunset is " . $sunset . "<br>\nCurrent Temperature " . $currenttemp . "°C with " . $data["weather"]["0"]["description"] . "<br>\nMaximum Temperature " . $maxtemp . "°C<br>\nMinimum Temperature " . $mintemp . "°C<br>\nHumidity " . $humidity . "%<br>\nAtmospheric Pressure " . $pressure . "hPa<br>\nWind " . degToCompass($data["wind"]["deg"]) . " (" . $data["wind"]["deg"] . "°) at " . $windspeed . "km/hr<br>";
if ($rain != null) {
    echo "Rain in the last hour " . $rain . "mm<br>";
}
echo "</div>";
function degToCompass($num)
{
    $val = $num / 22.5 + 0.5;
    $arr = ["North", "North North East", "North East", "East North East", "East", "East South East", "South East", "South South East", "South", "South South West", "South West", "West South West", "West", "West North West", "North West", "North North West"];
    return $arr[floor($val)];
}
//var_dump($data);
switch (json_last_error()) {
    case JSON_ERROR_NONE:
        break;
    case JSON_ERROR_DEPTH:
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
//var_dump($phpObj);
$weather = new stdClass();
$weather->temp = $phpObj->query->results->channel->item->condition->temp;
$weather->condition = $phpObj->query->results->channel->item->condition->code;
$weather->condition_text = $condizioni[$phpObj->query->results->channel->item->condition->code];
$weather->condition_original_text = $phpObj->query->results->channel->item->condition->text;
$weather->sunrise = $phpObj->query->results->channel->astronomy->sunrise;
$weather->sunset = $phpObj->query->results->channel->astronomy->sunset;
$weather->humidity = $phpObj->query->results->channel->atmosphere->humidity;
$weather->wind_speed = $phpObj->query->results->channel->wind->speed;
$weather->wind_direction = degToCompass($phpObj->query->results->channel->wind->direction);
$weather->wind_direction_deg = $phpObj->query->results->channel->wind->direction;
$weather->forecast_code = $phpObj->query->results->channel->item->forecast[0]->code;
$weather->forecast_high_temp = $phpObj->query->results->channel->item->forecast[0]->high;
$weather->forecast_low_temp = $phpObj->query->results->channel->item->forecast[0]->low;
$weather->forecast_icon = $icone_condizioni[$weather->forecast_code];
$weather->forecast = $phpObj->query->results->channel->item->forecast;
foreach ($weather->forecast as &$forecast) {
    $forecast->icon = $icone_condizioni[$forecast->code];
}
echo json_encode($weather);
//echo "\nMeteo per Rimini\n";
//echo "----------------\n";
//echo "Temperatura:      ".$phpObj->query->results->channel->item->condition->temp."° C\n";
//echo "Condizioni meteo: ".$condizioni[$phpObj->query->results->channel->item->condition->code]."\n";
//echo "Alba:             ".$phpObj->query->results->channel->astronomy->sunrise."\n";