function outputForecast($saddress, $city, $state, $sunit)
{
    $googleurl = "https://maps.googleapis.com/maps/api/geocode/xml?key=AIzaSyDISWh9GHBOX2VYSgHw0969f-G6ShYYpMQ&address=";
    $address = $saddress . ',' . $city . ',' . $state;
    $geocodeurl = $googleurl . urlencode($address);
    try {
        $response_xml_data = @file_get_contents($geocodeurl);
    } catch (Exception $e) {
        echo '<script language="javascript">';
        echo 'alert("', $e->getMessage(), '")';
        echo '</script>';
        return;
    }
    if ($response_xml_data == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("Fetching address failed.Please try again!")';
        echo '</script>';
        return;
    }
    $simplexml = simplexml_load_string($response_xml_data);
    $lats = $simplexml->xpath('//location/lat');
    $lngs = $simplexml->xpath('//location/lng');
    $formataddress = $simplexml->xpath('//status');
    if (count($lats) == 0 || count($lngs) == 0 || strpos($formataddress[0], 'ZERO_RESULTS') == true) {
        echo '<div id="forecast" class="diverror">';
        echo "<h2>No results returned!</h2>\n <h2>Please try another address.</h2>";
        echo '</div>';
        return;
    }
    $lat = $lats[0];
    $lng = $lngs[0];
    $forecasturl = "https://api.forecast.io/forecast/f2f049b51c218c7578dfccf9b55c2b08/";
    $urlpram = $lat . ',' . $lng . '?units=' . $sunit . '&exclude=flags';
    $forecasturl .= $urlpram;
    try {
        $response_xml_data = @file_get_contents($forecasturl);
    } catch (Exception $e) {
        echo '<script language="javascript">';
        echo 'alert("', $e->getMessage(), '")';
        echo '</script>';
        return;
    }
    if ($response_xml_data == FALSE) {
        echo '<script language="javascript">';
        echo 'alert("Fetching weather forcast failed.Please try again!")';
        echo '</script>';
        return;
    }
    $jsonObject = json_decode($response_xml_data, true);
    $wCondition = $jsonObject['currently']['summary'];
    $wTemp = convertTemp($jsonObject['currently']['temperature'], $sunit);
    $wicon = converticon($jsonObject['currently']['icon'], $wCondition);
    $wprecp = convertprecep($jsonObject['currently']['precipIntensity'], $sunit);
    $wprecprob = convertpercernt($jsonObject['currently']['precipProbability']);
    $wwspeed = convertwindspeed($jsonObject['currently']['windSpeed'], $sunit);
    $wdewpoint = round($jsonObject['currently']['dewPoint']);
    $whumid = convertpercernt($jsonObject['currently']['humidity']);
    if (isset($jsonObject['currently']['visibility'])) {
        $wvisib = convertvisibility($jsonObject['currently']['visibility'], $sunit);
    } else {
        $wvisib = convertvisibility(0, $sunit);
    }
    $wsunrise = convertTime($jsonObject['daily']['data'][0]['sunriseTime'], $jsonObject['timezone']);
    $wsunset = convertTime($jsonObject['daily']['data'][0]['sunsetTime'], $jsonObject['timezone']);
    echo '<div id="forecast" class="divtable"><h2>' . $wCondition . '</h2><h2>' . $wTemp . '</h2>' . $wicon;
    echo '<table><tr><td>Precipitation:</td><td>' . $wprecp . '</td></tr>';
    echo '<tr><td>Chance of Rain:</td><td>' . $wprecprob . '</td></tr>';
    echo '<tr><td>Wind Speed:</td><td>' . $wwspeed . '</td></tr>';
    echo '<tr><td>Dew Point:</td><td>' . $wdewpoint . '</td></tr>';
    echo '<tr><td>Humidity:</td><td>' . $whumid . '</td></tr>';
    echo '<tr><td>Visibility:</td><td>' . $wvisib . '</td></tr>';
    echo '<tr><td>Sunrise:</td><td>' . $wsunrise . '</td></tr>';
    echo '<tr><td>Sunset:</td><td>' . $wsunset . '</td></tr></table></div>';
}
Пример #2
0
function getdaily($result, $unit)
{
    $darray = $result['daily']['data'];
    $dobj = [];
    for ($i = 1; $i < count($darray); ++$i) {
        $table = array('Day' => date("l", $darray[$i]['time']), 'Month Date' => date("M d", $darray[$i]['time']), 'Icon Image' => converticon($darray[$i]['icon']), 'Min Temp' => convertTemp($darray[$i]['temperatureMin'], $unit), 'Max Temp' => convertTemp($darray[$i]['temperatureMax'], $unit), 'Summary' => $darray[$i]['summary'], 'Humidity' => convertpercernt($darray[$i]['humidity']), 'Visibility' => convertvisibility($darray[$i], $unit), 'Wind Speed' => convertwindspeed($darray[$i], $unit), 'Sunrise' => convertTime($darray[$i]['sunriseTime'], $result['timezone']), 'Sunset' => convertTime($darray[$i]['sunsetTime'], $result['timezone']), 'Pressure' => convertpressure($darray[$i]['pressure'], $unit));
        array_push($dobj, $table);
    }
    return $dobj;
}