PHP & Web Services – using SOAP to get meteorological data for a location

1. The plan

So you want to show cool real time meteorological data on your website. In fact, you would like to show a five–day forecast for your exact location, and use the same images as NOAA use on their website. There are many ways of doing this, but seeing as I like PHP5 and I have been wanting to explore web–services, I decided to try and use PHP5's built–in SOAP functions. Luckily for me, NOAA already provides weather forecasts using Web Services Description Language (WSDL).

2. Ingredients — configuring PHP

First, you need to compile PHP5 with SOAP enabled. This is not the default. It is done by adding:

--enable-soap

to your compile command. You can test whether the SOAP functions are available by doing a couple of simple tests.

In your php.ini file there are a series of SOAP-related options, which I have set to the following values for testing purposes:

[soap]
; Enables or disables WSDL caching feature.
soap.wsdl_cache_enabled=0
; Sets the directory name where SOAP extension will put cache files.
soap.wsdl_cache_dir="/tmp"
; (time to live) Sets the number of second while cached file will be used 
; instead of original one.
soap.wsdl_cache_ttl=0
; maximum number of in memory cached wsdl files
soap.wsdl_cache_limit=0

You basically want all caching turned off until you get the resultant XML just the way you want it.

3. Get the URL of the web–service

Once you have the URL, in this case http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl, you then need to instantiate a new SoapClient class. Using the code below does this, with the added bonus of the __getFunctions() function returning all the available functions for this web–service.

  1. <?php
  2. $weather = new SoapClient( "http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl" ) ;
  3. var_dump( $weather->__getFunctions() ) ;
  4. ?>

This query results in the following output:

  1. array(2) {
  2. [0]=>
  3. string(157) "string NDFDgen(decimal $latitude, decimal $longitude, productType $product, dateTime $startTime, dateTime $endTime, weatherParametersType $weatherParameters)"
  4. [1]=>
  5. string(113) "string NDFDgenByDay(decimal $latitude, decimal $longitude, date $startDate, integer $numDays, formatType $format)"
  6. }

This array shows the two functions available to you, NDFDgen and NDFDgenByDay and the various arguments they need in order to return some meaningful data to you. The web–service actually lists this on its page in the documentation tag:

The service has two exposed functions, NDFDgen and NDFDgenByDay. For the NDFDgen function, the client needs to provide a latitude and longitude pair and the product type. The client also needs to provide the start and end time of the period that it wants data for. For the time-series product, the client needs to provide an array of boolean values corresponding to which weather values should appear in the time series product. For the NDFDgenByDay function, the client needs to provide a latitude and longitude pair, the date it wants to start retrieving data for and the number of days worth of data. The client also needs to provide the format that is desired.

4. Define the metrics you want

From the NDFDgen function, you can see that one of the arguments is $weatherParameters. This is where you define what NOAA metrics you want to retrieve. There are many to choose from! The list below shows the available parameters. They need to be defined in an associative array:

Define this in your PHP code as follows:

  1. <?php
  2. $weatherParameters = array(
  3. "maxt" => true,
  4. "mint" => true,
  5. "temp" => true,
  6. "dew" => true,
  7. "pop12" => true,
  8. "qpf" => true,
  9. "sky" => true,
  10. "snow" => true,
  11. "wspd" => true,
  12. "wdir" => true,
  13. "wx" => true,
  14. "waveh" => true,
  15. "icons" => true,
  16. "rh" => true,
  17. "appt" => true,
  18. "incw34" => true,
  19. "incw50" => true,
  20. "incw64" => true,
  21. "cumw34" => true,
  22. "cumw50" => true,
  23. "cumw64" => true,
  24. "conhazo" => true,
  25. "ptornado" => true,
  26. "phail" => true,
  27. "ptstmwinds" => true,
  28. "pxtornado" => true,
  29. "pxhail" => true,
  30. "pxtstmwinds" => true,
  31. "ptotsvrtstm" => true,
  32. "pxtotsvrtstm" => true,
  33. "wgust" => true
  34. ) ;
  35. ?>

Now you are ready to make a basic SOAP request of this web–service!

5. Get the current day's weather for a latitude and longitude

The function NDFDgen requires:

  1. <?php
  2. $test = $weather->NDFDgen(
  3. "35.225",
  4. "-120.111",
  5. "time-series",
  6. "2007-07-06T12:00:00",
  7. "2007-07-07T12:00:00",
  8. $weatherParameters ) ;
  9. echo $test ;
  10. ?>

The resultant XML from this SOAP query is:

  1. <?xml version='1.0' ?>
  2. <dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
  5. <head>
  6. <product concise-name="time-series" operational-mode="official" srsName="WGS 1984">
  7. <title>NOAAs National Weather Service Forecast Data</title>
  8. <field>meteorological</field>
  9. <category>forecast</category>
  10. <creation-date refresh-frequency='PT1H'>2007-07-05T19:06:19Z</creation-date>
  11. </product>
  12. <source>
  13. <more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
  14. <production-center>Meteorological Development Laboratory <sub-center>Product Generation Branch</sub-center></production-center>
  15. <disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
  16. <credit>http://www.weather.gov/</credit>
  17. <credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
  18. <feedback>http://www.weather.gov/survey/nws-survey.php?code=xmlsoap</feedback>
  19. </source>
  20. </head>
  21. <data>
  22. <location>
  23. <location-key>point1</location-key>
  24. <point latitude="35.225" longitude="-120.111" />
  25. </location>
  26. <time-layout time-coordinate="local" summarization="none">
  27. <layout-key>k-p24h-n2-1</layout-key>
  28. <start-valid-time>2007-07-06T08:00:00-07:00</start-valid-time>
  29. <end-valid-time>2007-07-06T20:00:00-07:00</end-valid-time>
  30. <start-valid-time>2007-07-07T08:00:00-07:00</start-valid-time>
  31. <end-valid-time>2007-07-07T20:00:00-07:00</end-valid-time>
  32. </time-layout>
  33. <time-layout time-coordinate="local" summarization="none">
  34. <layout-key>k-p24h-n1-2</layout-key>
  35. <start-valid-time>2007-07-06T20:00:00-07:00</start-valid-time>
  36. <end-valid-time>2007-07-07T09:00:00-07:00</end-valid-time>
  37. </time-layout>
  38. <time-layout time-coordinate="local" summarization="none">
  39. <layout-key>k-p12h-n3-3</layout-key>
  40. <start-valid-time>2007-07-06T04:00:00-07:00</start-valid-time>
  41. <end-valid-time>2007-07-06T17:00:00-07:00</end-valid-time>
  42. <start-valid-time>2007-07-06T16:00:00-07:00</start-valid-time>
  43. <end-valid-time>2007-07-07T05:00:00-07:00</end-valid-time>
  44. <start-valid-time>2007-07-07T04:00:00-07:00</start-valid-time>
  45. <end-valid-time>2007-07-07T17:00:00-07:00</end-valid-time>
  46. </time-layout>
  47. <time-layout time-coordinate="local" summarization="none">
  48. <layout-key>k-p24h-n2-4</layout-key>
  49. <start-valid-time>2007-07-06T05:00:00-07:00</start-valid-time>
  50. <end-valid-time>2007-07-07T05:00:00-07:00</end-valid-time>
  51. <start-valid-time>2007-07-07T05:00:00-07:00</start-valid-time>
  52. <end-valid-time>2007-07-08T05:00:00-07:00</end-valid-time>
  53. </time-layout>
  54. <time-layout time-coordinate="local" summarization="none">
  55. <layout-key>k-p3h-n9-5</layout-key>
  56. <start-valid-time>2007-07-06T14:00:00-07:00</start-valid-time>
  57. <start-valid-time>2007-07-06T17:00:00-07:00</start-valid-time>
  58. <start-valid-time>2007-07-06T20:00:00-07:00</start-valid-time>
  59. <start-valid-time>2007-07-06T23:00:00-07:00</start-valid-time>
  60. <start-valid-time>2007-07-07T02:00:00-07:00</start-valid-time>
  61. <start-valid-time>2007-07-07T05:00:00-07:00</start-valid-time>
  62. <start-valid-time>2007-07-07T08:00:00-07:00</start-valid-time>
  63. <start-valid-time>2007-07-07T11:00:00-07:00</start-valid-time>
  64. <start-valid-time>2007-07-07T14:00:00-07:00</start-valid-time>
  65. </time-layout>
  66. <time-layout time-coordinate="local" summarization="none">
  67. <layout-key>k-p6h-n5-6</layout-key>
  68. <start-valid-time>2007-07-06T10:00:00-07:00</start-valid-time>
  69. <end-valid-time>2007-07-06T17:00:00-07:00</end-valid-time>
  70. <start-valid-time>2007-07-06T16:00:00-07:00</start-valid-time>
  71. <end-valid-time>2007-07-06T23:00:00-07:00</end-valid-time>
  72. <start-valid-time>2007-07-06T22:00:00-07:00</start-valid-time>
  73. <end-valid-time>2007-07-07T05:00:00-07:00</end-valid-time>
  74. <start-valid-time>2007-07-07T04:00:00-07:00</start-valid-time>
  75. <end-valid-time>2007-07-07T11:00:00-07:00</end-valid-time>
  76. <start-valid-time>2007-07-07T10:00:00-07:00</start-valid-time>
  77. <end-valid-time>2007-07-07T17:00:00-07:00</end-valid-time>
  78. </time-layout>
  79. <time-layout time-coordinate="local" summarization="none">
  80. <layout-key>k-p3h-n1-7</layout-key>
  81. <start-valid-time>2007-07-06T13:00:00-07:00</start-valid-time>
  82. <end-valid-time>2007-07-06T17:00:00-07:00</end-valid-time>
  83. </time-layout>
  84.  
  85. <parameters applicable-location="point1">
  86. <temperature type='maximum' units="Fahrenheit" time-layout="k-p24h-n2-1">
  87. <name>Daily Maximum Temperature</name>
  88. <value>97</value>
  89. <value>95</value>
  90. </temperature>
  91. <temperature type='minimum' units="Fahrenheit" time-layout="k-p24h-n1-2">
  92. <name>Daily Minimum Temperature</name>
  93. <value>61</value>
  94. </temperature>
  95. <temperature type='hourly' units="Fahrenheit" time-layout="k-p3h-n9-5">
  96. <name>Temperature</name>
  97. <value>95</value>
  98. <value>96</value>
  99. <value>88</value>
  100. <value>76</value>
  101. <value>67</value>
  102. <value>62</value>
  103. <value>71</value>
  104. <value>86</value>
  105. <value>93</value>
  106. </temperature>
  107. <temperature type='dew point' units="Fahrenheit" time-layout="k-p3h-n9-5">
  108. <name>Dew Point Temperature</name>
  109. <value>38</value>
  110. <value>38</value>
  111. <value>39</value>
  112. <value>40</value>
  113. <value>40</value>
  114. <value>41</value>
  115. <value>41</value>
  116. <value>42</value>
  117. <value>41</value>
  118. </temperature>
  119. <temperature type='apparent' units="Fahrenheit" time-layout="k-p3h-n9-5">
  120. <name>Apparent Temperature</name>
  121. <value>91</value>
  122. <value>93</value>
  123. <value>85</value>
  124. <value>76</value>
  125. <value>67</value>
  126. <value>62</value>
  127. <value>71</value>
  128. <value>83</value>
  129. <value>89</value>
  130. </temperature>
  131. <precipitation type='liquid' units="inches" time-layout="k-p6h-n5-6">
  132. <name>Liquid Precipitation Amount</name>
  133. <value>0.00</value>
  134. <value>0.00</value>
  135. <value>0.00</value>
  136. <value>0.00</value>
  137. <value>0.00</value>
  138. </precipitation>
  139. <precipitation type='snow' units="inches" time-layout="k-p3h-n1-7">
  140. <name>Snow Amount</name>
  141. <value>0</value>
  142. </precipitation>
  143. <probability-of-precipitation type='12 hour' units="percent" time-layout="k-p12h-n3-3">
  144. <name>12 Hourly Probability of Precipitation</name>
  145. <value>0</value>
  146. <value>0</value>
  147. <value>0</value>
  148. </probability-of-precipitation>
  149. <convective-hazard>
  150. <outlook time-layout="k-p24h-n2-4">
  151. <name>Convective Hazard Outlook</name>
  152. <value>no thunderstorms</value>
  153. <value>no thunderstorms</value>
  154. </outlook>
  155. </convective-hazard>
  156. <convective-hazard>
  157. <severe-component type="severe thunderstorms" units="percent" time-layout="k-p24h-n2-4">
  158. <name>Total Probability of Severe Thunderstorms</name>
  159. <value>0</value>
  160. <value>0</value>
  161. </severe-component>
  162. </convective-hazard>
  163. <convective-hazard>
  164. <severe-component type="extreme severe thunderstorms" units="percent" time-layout="k-p24h-n2-4">
  165. <name>Total Probability of Extreme Severe Thunderstorms</name>
  166. <value>0</value>
  167. <value>0</value>
  168. </severe-component>
  169. </convective-hazard>
  170. <wind-speed type="incremental34" units="percent" time-layout="k-p6h-n5-6">
  171. <name>Probability of a Tropical Cyclone Wind Speed >34 Knots (Incremental)</name>
  172. <value>0</value>
  173. <value>0</value>
  174. <value>0</value>
  175. <value>0</value>
  176. <value>0</value>
  177. </wind-speed>
  178. <wind-speed type="incremental50" units="percent" time-layout="k-p6h-n5-6">
  179. <name>Probability of a Tropical Cyclone Wind Speed >50 Knots (Incremental)</name>
  180. <value>0</value>
  181. <value>0</value>
  182. <value>0</value>
  183. <value>0</value>
  184. <value>0</value>
  185. </wind-speed>
  186. <wind-speed type="incremental64" units="percent" time-layout="k-p6h-n5-6">
  187. <name>Probability of a Tropical Cyclone Wind Speed >64 Knots (Incremental)</name>
  188. <value>0</value>
  189. <value>0</value>
  190. <value>0</value>
  191. <value>0</value>
  192. <value>0</value>
  193. </wind-speed>
  194. <wind-speed type="cumulative34" units="percent" time-layout="k-p6h-n5-6">
  195. <name>Probability of a Tropical Cyclone Wind Speed >34 Knots (Cumulative)</name>
  196. <value>0</value>
  197. <value>0</value>
  198. <value>0</value>
  199. <value>0</value>
  200. <value>0</value>
  201. </wind-speed>
  202. <wind-speed type="cumulative50" units="percent" time-layout="k-p6h-n5-6">
  203. <name>Probability of a Tropical Cyclone Wind Speed >50 Knots (Cumulative)</name>
  204. <value>0</value>
  205. <value>0</value>
  206. <value>0</value>
  207. <value>0</value>
  208. <value>0</value>
  209. </wind-speed>
  210. <wind-speed type="cumulative64" units="percent" time-layout="k-p6h-n5-6">
  211. <name>Probability of a Tropical Cyclone Wind Speed >64 Knots (Cumulative)</name>
  212. <value>0</value>
  213. <value>0</value>
  214. <value>0</value>
  215. <value>0</value>
  216. <value>0</value>
  217. </wind-speed>
  218. <wind-speed type="sustained" units="knots" time-layout="k-p3h-n9-5">
  219. <name>Wind Speed</name>
  220. <value>12</value>
  221. <value>12</value>
  222. <value>10</value>
  223. <value>10</value>
  224. <value>2</value>
  225. <value>2</value>
  226. <value>2</value>
  227. <value>2</value>
  228. <value>7</value>
  229. </wind-speed>
  230. <wind-speed type="gust" units="knots" time-layout="k-p3h-n9-5">
  231. <name>Wind Speed Gust</name>
  232. <value>12</value>
  233. <value>12</value>
  234. <value>10</value>
  235. <value>10</value>
  236. <value>2</value>
  237. <value>2</value>
  238. <value>2</value>
  239. <value>2</value>
  240. <value>7</value>
  241. </wind-speed>
  242. <direction type='wind' units="degrees true" time-layout="k-p3h-n9-5">
  243. <name>Wind Direction</name>
  244. <value>226</value>
  245. <value>226</value>
  246. <value>238</value>
  247. <value>238</value>
  248. <value>14</value>
  249. <value>14</value>
  250. <value>326</value>
  251. <value>326</value>
  252. <value>290</value>
  253. </direction>
  254. <cloud-amount type='total' units="percent" time-layout="k-p3h-n9-5">
  255. <name>Cloud Cover Amount</name>
  256. <value>0</value>
  257. <value>0</value>
  258. <value>0</value>
  259. <value>0</value>
  260. <value>0</value>
  261. <value>0</value>
  262. <value>0</value>
  263. <value>0</value>
  264. <value>0</value>
  265. </cloud-amount>
  266. <humidity type='relative' units="percent" time-layout="k-p3h-n9-5">
  267. <name>Relative Humidity</name>
  268. <value>14</value>
  269. <value>13</value>
  270. <value>18</value>
  271. <value>27</value>
  272. <value>38</value>
  273. <value>46</value>
  274. <value>34</value>
  275. <value>21</value>
  276. <value>16</value>
  277. </humidity>
  278. <weather time-layout="k-p3h-n9-5">
  279. <name>Weather Type, Coverage, and Intensity</name>
  280. <weather-conditions />
  281. <weather-conditions />
  282. <weather-conditions />
  283. <weather-conditions />
  284. <weather-conditions />
  285. <weather-conditions />
  286. <weather-conditions />
  287. <weather-conditions />
  288. <weather-conditions />
  289. </weather>
  290. <conditions-icon type="forecast-NWS" time-layout="k-p3h-n9-5">
  291. <name>Conditions Icons</name>
  292. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  293. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  294. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  295. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/nskc.jpg</icon-link>
  296. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/nskc.jpg</icon-link>
  297. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/nskc.jpg</icon-link>
  298. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  299. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  300. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  301. </conditions-icon>
  302. <water-state time-layout="k-p12h-n3-3">
  303. <waves type="significant" units="feet">
  304. <name>Wave Height</name>
  305. <value xsi:nil="true" />
  306. <value xsi:nil="true" />
  307. <value xsi:nil="true" />
  308. </waves>
  309. </water-state>
  310. </parameters>
  311. </data>
  312. </dwml>
  313.  

So with this code you get all the most-recent metrics recorded in a nicely formatted XML file. You now need to parse this XML file (see step 6 below).

5. Get an n–day weather forecast for a latitude and longitude

This function, NDFDgenByDay, requires similar parameters including the latitude and longitude as defined in the previous function, along with a couple of important new ones.

The time stamp

It only requires a time stamp for the current day, in the format described above. I have noticed some oddities with this time stamp argument. If you set it for some time in the past then it defaults the first day returned as today at 06:00. If you set it for some time in the future, then it returns that day, starting at 06:00. You can actually leave it as an empty string ('') and it will default to the current day.

If you put a time stamp that is 7 or greater days into the future, then the query fails and you get the times returned from 1970-01-01, ie. junk. An example is shown below. I inserted the time string '2007-07-20T12:00:00' on July 5th, 2007 and got the following as part of the output:

  1. <time-layout time-coordinate="local" summarization="24hourly">
  2. <layout-key>k-p24h-n10-1</layout-key>
  3. <start-valid-time>1970-01-01T23:59:00-08:00</start-valid-time>
  4. <end-valid-time>1970-01-01T23:59:00-08:00</end-valid-time>
  5. <start-valid-time>1970-01-02T23:59:00-08:00</start-valid-time>
  6.  
  7. <end-valid-time>1970-01-02T23:59:00-08:00</end-valid-time>
  8. <start-valid-time>1970-01-03T23:59:00-08:00</start-valid-time>
  9. <end-valid-time>1970-01-03T23:59:00-08:00</end-valid-time>
  10. <start-valid-time>1970-01-04T23:59:00-08:00</start-valid-time>
  11. <end-valid-time>1970-01-04T23:59:00-08:00</end-valid-time>
  12. <start-valid-time>1970-01-05T23:59:00-08:00</start-valid-time>
  13.  
  14. <end-valid-time>1970-01-05T23:59:00-08:00</end-valid-time>
  15. <start-valid-time>1970-01-06T23:59:00-08:00</start-valid-time>
  16. <end-valid-time>1970-01-06T23:59:00-08:00</end-valid-time>
  17. <start-valid-time>1970-01-07T23:59:00-08:00</start-valid-time>
  18. <end-valid-time>1970-01-07T23:59:00-08:00</end-valid-time>
  19. <start-valid-time>1970-01-08T23:59:00-08:00</start-valid-time>
  20.  
  21. <end-valid-time>1970-01-08T23:59:00-08:00</end-valid-time>
  22. <start-valid-time>1970-01-09T23:59:00-08:00</start-valid-time>
  23. <end-valid-time>1970-01-09T23:59:00-08:00</end-valid-time>
  24. <start-valid-time>1970-01-10T23:59:00-08:00</start-valid-time>
  25. <end-valid-time>1970-01-10T23:59:00-08:00</end-valid-time>
  26. </time-layout>

Number of days

The fourth argument is the number of days to return weather information for. In this case, I have it set to 5 days. You can put in n-many days, but the maximum that will get returned to you is 7. The extra days values will be empty strings (see example below where I requested 20 days of data).

  1. <temperature type='maximum' units="Fahrenheit" time-layout="k-p24h-n20-1">
  2. <name>Daily Maximum Temperature</name>
  3. <value>105</value>
  4. <value>97</value>
  5. <value>95</value>
  6. <value>92</value>
  7. <value>92</value>
  8. <value>92</value>
  9. <value>92</value>
  10. <value xsi:nil="true" />
  11. <value xsi:nil="true" />
  12. <value xsi:nil="true" />
  13. <value xsi:nil="true" />
  14. <value xsi:nil="true" />
  15. <value xsi:nil="true" />
  16. <value xsi:nil="true" />
  17. <value xsi:nil="true" />
  18. <value xsi:nil="true" />
  19. <value xsi:nil="true" />
  20. <value xsi:nil="true" />
  21. <value xsi:nil="true" />
  22. <value xsi:nil="true" />
  23. </temperature>

Format

The final argument is the format in which you want the data. I have specified '24 hourly', but the other format type is '12 hourly'.

Full example

  1. <?php
  2. $test = $weather->NDFDgenByDay( "35.225","-120.111", "2007-07-05T12:00:00", 5, "24 hourly" ) ;
  3. echo $test ;
  4. ?>

The resultant XML from this SOAP query is:

  1. <?xml version='1.0' ?>
  2. <dwml version='1.0' xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
  5. <head>
  6. <product concise-name="dwmlByDay" operational-mode="official" srsName="WGS 1984">
  7. <title>NOAAs National Weather Service Forecast by 24 Hour period</title>
  8. <field>meteorological</field>
  9. <category>forecast</category>
  10. <creation-date refresh-frequency='PT1H'>2007-07-05T21:37:32Z</creation-date>
  11. </product>
  12. <source>
  13. <more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
  14. <production-center>Meteorological Development Laboratory <sub-center>Product Generation Branch</sub-center></production-center>
  15. <disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
  16. <credit>http://www.weather.gov/</credit>
  17.  
  18. <credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
  19. <feedback>http://www.weather.gov/survey/nws-survey.php?code=xmlsoap</feedback>
  20. </source>
  21. </head>
  22. <data>
  23. <location>
  24. <location-key>point1</location-key>
  25. <point latitude="35.225" longitude="-120.111" />
  26. </location>
  27. <time-layout time-coordinate="local" summarization="24hourly">
  28. <layout-key>k-p24h-n5-1</layout-key>
  29. <start-valid-time>2007-07-05T06:00:00-07:00</start-valid-time>
  30. <end-valid-time>2007-07-06T06:00:00-07:00</end-valid-time>
  31. <start-valid-time>2007-07-06T06:00:00-07:00</start-valid-time>
  32. <end-valid-time>2007-07-07T06:00:00-07:00</end-valid-time>
  33. <start-valid-time>2007-07-07T06:00:00-07:00</start-valid-time>
  34. <end-valid-time>2007-07-08T06:00:00-07:00</end-valid-time>
  35. <start-valid-time>2007-07-08T06:00:00-07:00</start-valid-time>
  36. <end-valid-time>2007-07-09T06:00:00-07:00</end-valid-time>
  37. <start-valid-time>2007-07-09T06:00:00-07:00</start-valid-time>
  38. <end-valid-time>2007-07-10T06:00:00-07:00</end-valid-time>
  39. </time-layout>
  40. <time-layout time-coordinate="local" summarization="12hourly">
  41. <layout-key>k-p12h-n10-2</layout-key>
  42. <start-valid-time>2007-07-05T06:00:00-07:00</start-valid-time>
  43. <end-valid-time>2007-07-05T18:00:00-07:00</end-valid-time>
  44. <start-valid-time>2007-07-05T18:00:00-07:00</start-valid-time>
  45. <end-valid-time>2007-07-06T06:00:00-07:00</end-valid-time>
  46. <start-valid-time>2007-07-06T06:00:00-07:00</start-valid-time>
  47. <end-valid-time>2007-07-06T18:00:00-07:00</end-valid-time>
  48. <start-valid-time>2007-07-06T18:00:00-07:00</start-valid-time>
  49. <end-valid-time>2007-07-07T06:00:00-07:00</end-valid-time>
  50. <start-valid-time>2007-07-07T06:00:00-07:00</start-valid-time>
  51. <end-valid-time>2007-07-07T18:00:00-07:00</end-valid-time>
  52. <start-valid-time>2007-07-07T18:00:00-07:00</start-valid-time>
  53. <end-valid-time>2007-07-08T06:00:00-07:00</end-valid-time>
  54. <start-valid-time>2007-07-08T06:00:00-07:00</start-valid-time>
  55. <end-valid-time>2007-07-08T18:00:00-07:00</end-valid-time>
  56. <start-valid-time>2007-07-08T18:00:00-07:00</start-valid-time>
  57. <end-valid-time>2007-07-09T06:00:00-07:00</end-valid-time>
  58. <start-valid-time>2007-07-09T06:00:00-07:00</start-valid-time>
  59. <end-valid-time>2007-07-09T18:00:00-07:00</end-valid-time>
  60. <start-valid-time>2007-07-09T18:00:00-07:00</start-valid-time>
  61. <end-valid-time>2007-07-10T06:00:00-07:00</end-valid-time>
  62. </time-layout>
  63.  
  64. <parameters applicable-location="point1">
  65. <temperature type='maximum' units="Fahrenheit" time-layout="k-p24h-n5-1">
  66. <name>Daily Maximum Temperature</name>
  67. <value>105</value>
  68. <value>97</value>
  69. <value>95</value>
  70. <value>92</value>
  71. <value>92</value>
  72. </temperature>
  73. <temperature type='minimum' units="Fahrenheit" time-layout="k-p24h-n5-1">
  74. <name>Daily Minimum Temperature</name>
  75. <value>61</value>
  76. <value>61</value>
  77. <value>59</value>
  78. <value>57</value>
  79. <value>58</value>
  80. </temperature>
  81. <probability-of-precipitation type='12 hour' units="percent" time-layout="k-p12h-n10-2">
  82. <name>12 Hourly Probability of Precipitation</name>
  83. <value>0</value>
  84. <value>0</value>
  85. <value>0</value>
  86. <value>0</value>
  87. <value>0</value>
  88. <value>0</value>
  89. <value>0</value>
  90. <value>0</value>
  91. <value>0</value>
  92. <value>0</value>
  93. </probability-of-precipitation>
  94. <weather time-layout="k-p24h-n5-1">
  95. <name>Weather Type, Coverage, and Intensity</name>
  96. <weather-conditions weather-summary="Hot">
  97. </weather-conditions>
  98. <weather-conditions weather-summary="Hot">
  99. </weather-conditions>
  100. <weather-conditions weather-summary="Sunny">
  101. </weather-conditions>
  102. <weather-conditions weather-summary="Sunny">
  103. </weather-conditions>
  104. <weather-conditions weather-summary="Sunny">
  105. </weather-conditions>
  106. </weather>
  107.  
  108. <conditions-icon type="forecast-NWS" time-layout="k-p24h-n5-1">
  109. <name>Conditions Icons</name>
  110. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/hot.jpg</icon-link>
  111. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/hot.jpg</icon-link>
  112. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  113. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  114. <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/skc.jpg</icon-link>
  115. </conditions-icon>
  116. </parameters>
  117. </data>
  118. </dwml>
  119.  

6. What do I do now I have the XML data?

Now you need to write an XML–parser to get the data you want from the returned XML file. This is beyond the scope of this tutorial, and there are many examples on the web of how to do this. My favorite method is to use PHP5's DOMDocument functions.

One of the simplest ways of displaying this data on your own website is to get the content of the conditions-icon tag. This provides links to NOAA–built icons that describe the weather conditions at the location you defined. However, you can also take other values and create graphs, your own custom icons based on the weather conditions summary, or whatever you like. The sky is the limit (ahem).

7. References

Here are some references that I found useful in getting up and running with this: