-
Notifications
You must be signed in to change notification settings - Fork 10
/
getWeather.php
79 lines (64 loc) · 2.54 KB
/
getWeather.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
$lat = 34.0416;
$long = -118.2988;
$zipCode = $_REQUEST['zipCode'];
// or use this: http://graphical.weather.gov/xml/rest.php
//$url = "http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?whichClient=NDFDgen&lat=$lat&lon=$long&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=¢erPointLat=¢erPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=§or=&gmlListLatLon=&featureType=&requestedTime=&startTime=&endTime=&compType=&propertyName=&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2017-06-28T00%3A00%3A00&Unit=e&maxt=maxt&mint=mint&temp=temp&Submit=Submit";
// currently using yahoo
$url = "http://weather.yahooapis.com/forecastrss?p=$zipCode";
$response = file_get_contents($url);
$responseData = array();
//echo $response;
$dom = new DOMDocument();
@$dom->loadHTML($response);
$x = new DOMXPath($dom);
// get current condition
$nodeList=$x->query("//condition");
if($nodeList->length>0)
{
$currentTemp = $nodeList->item(0)->getAttribute('temp');
$responseData['currentTemp'] = $currentTemp.'°';
}
// get day, date, low, high, and text
$count=1;
$otherDays = "";
foreach($x->query("//forecast") as $node)
{
$day = $node->getAttribute('day');
if($day=='Sat')
$day = 'Saturday';
if($day=='Sun')
$day = 'Sunday';
if($day=='Mon')
$day = 'Monday';
if($day=='Tue')
$day = 'Tuesday';
if($day=='Wed')
$day = 'Wednesday';
if($day=='Thu')
$day = 'Thursday';
if($day=='Fri')
$day = 'Friday';
//$date = $node->getAttribute('date');
//$date = str_replace('2013', '', $date);
$low = $node->getAttribute('low');
$high = $node->getAttribute('high');
$text = $node->getAttribute('text');
$code = $node->getAttribute('code');
if($count==1){
$responseData['hiloToday'] = "<span class='low'>$low°</span> - <span class='high'>$high°</span> today";
}
else
$otherDays.= "
<div class='weatherRow'>
<div class='weatherDay'>$day</div>
<div class='weatherImg'><img src='sun.png'></div>
<div class='tempHigh'><span class='high'>$high°</span></div>
<div class='tempLow'><span class='low'>$low°</span></div>
</div>
";
$count++;
}
$responseData['otherDays'] = $otherDays;
echo json_encode($responseData);
?>