-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathback_end.php
70 lines (45 loc) · 2 KB
/
back_end.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
header("Access-Control-Allow-Origin: *");
$googleApiKey = "AIzaSyBWCtABzNgXaPblRlhGdLxmjc3eT_xXbMU";
$gMapStr = urlencode("https://maps.googleapis.com/maps/api/geocode/xml?address=" . $_GET["sa"] . "," . $_GET["city"] . "," . $_GET["states"] . "&key=" . $googleApiKey);
//echo $gMapStr;
$xml=simplexml_load_file("$gMapStr") or die("Error: Cannot create object");
//echo $xml->status;
if ($xml->status === "ZERO_RESULTS") {
exit("There is no result returned by Google Map");
}
//var_dump(xml);
$lat = $xml->result->geometry->location->lat;
//echo $lat;
$lng = $xml->result->geometry->location->lng;
//echo $lng;
$apiKey = "17890a9eefa0dd3ece1f29b9e4524036";
$fcStr = "https://api.forecast.io/forecast/" . $apiKey . "/" . $lat. "," . "$lng" . "?units=" . $_GET['unit'] . "&exclude=flags";
//echo $fcStr;
$json = file_get_contents($fcStr);
$jsonDoc = json_decode($json, true);
$l_timezone = 'America/Los_Angeles';
//echo $l_timezone;
date_default_timezone_set($jsonDoc["timezone"]);
for ($i = 0; $i <= 7; $i++) {
$jsonDoc["daily"]["data"][$i]["sunriseTime"] = strftime("%I:%M %p", $jsonDoc["daily"]["data"][$i]["sunriseTime"]);
$jsonDoc["daily"]["data"][$i]["sunsetTime"] = strftime("%I:%M %p", $jsonDoc["daily"]["data"][$i]["sunsetTime"]);
//echo $jsonDoc["daily"]["data"][$i]["sunriseTime"];
//echo $jsonDoc["daily"]["data"][$i]["sunsetTime"];
}
for ($i = 0; $i < 24; $i++) {
$jsonDoc["hourly"]["data"][$i]["time"] = strftime("%I:%M %p", $jsonDoc["hourly"]["data"][$i]["time"]);
}
for ($i = 1; $i <= 7; $i++) {
date_default_timezone_set($jsonDoc["timezone"]);
$time = $jsonDoc["daily"]["data"][$i]["time"];
//echo $time . '\n';
$timeStr = strftime("%Y-%m-%d", $time);
//echo $timeStr;
date_default_timezone_set($l_timezone);
$date = new DateTime($timeStr);
$jsonDoc["daily"]["data"][$i]["time"] = $date->getTimestamp();
//echo $jsonDoc["daily"]["data"][$i]["time"] . '\n';
}
echo json_encode($jsonDoc);
?>