jwallace.us

tech, tunes, and other stuff

With PHP, SOAP Is - Well - Simple!

SOAP stands for Simple Object Access Protocol.  In a nutshell, it is a way for applications to communicate over HTTP using XML.

What I have done is set up a simple example that, given a zip code, will use SOAP to connect to NOAA to retrieve the current weather.  Rather than give a wordy explanation, I’ll just post the code below.  For simplicity I’ve made this a version that works from the command line.  You can download the code below here:  Soapy Weather CLI.  I have a web based version here that you can try out:  Soapy Weather

Soapy Weather
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
<?php
   # This demonstrates how easy it is to use SOAP.
   # -------------------------------------------------------------------------------
   # For more information on SOAP, go to:
   #           http://www.w3schools.com/soap/default.asp
   # For more information on WSDLs, go to:
   #           http://www.w3schools.com/wsdl/default.asp
   # -------------------------------------------------------------------------------
   # John Wallace - September 28, 2011 - http://jwallace.us

   echo PHP_EOL . "Soapy Weather (cli version)" . PHP_EOL . PHP_EOL;
   echo 'Enter your ZIP code > ';

   # get the zip code
   $str = fread(STDIN, 80);
   $zip_code = trim($str);

   # check zip code for non-numbers
   if (!ctype_digit($zip_code)) {
      die ("error: non-numbers were used in the zip code" . PHP_EOL);
   }
   echo PHP_EOL;

   # create a SOAP client using NOAA's WSDL
   $client = new SoapClient
             ("http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl");
   $dxml = new SimpleXMLElement($client->LatLonListZipCode($zip_code));
   # get the latitude and longitude of the zip code
   $location = preg_split ('/(,)/',$dxml->latLonList);

   # setting a time zone is required to use the date function
   date_default_timezone_set('America/Chicago');
   $dateTime = date("c");

   # get the current weather
   $dxml = new SimpleXMLElement ($client->NDFDgen($location[0],
                                 $location[1],
                                 'time-series',
                                 $dateTime,
                                 $dateTime,
                                 'e',
                                 'temp'));
   echo "Current conditions for $zip_code
                   (" . $dxml->data->location->point['latitude'] . "," .
                        $dxml->data->location->point['longitude'] . ") are: "
                        . PHP_EOL . PHP_EOL;

   # now parse and present the weather data returned in an XML format
   foreach ($dxml->data->parameters as $parameter) {
      foreach ($parameter as $element) {
         if (count($element) > 1) {
            if ($element->value != "") {
               echo $element->name . ": [" .
                    $element->value . "]" . PHP_EOL;
            }
         }
         else {
            foreach ($element as $sub_element) {
               if ($element->value != "") {
                  echo $sub_element->name . ": [" .
                       $sub_element->value . "]" . PHP_EOL;
               }
            }
         }
      }
   }
   echo PHP_EOL . "Done!" . PHP_EOL;
?>