Building XML Web Services with PHP NuSOAP - Walkthrough Example 2 (Page 4 of 5 )
Create a Web Service that will provide a list of all ATM machines for a given zip code and develop a web site with PHP that consumes this service. Upon providing a ZIP code, user can get a list of ATM services (Figure 3).
Figure 3
You can find a Web Service named GeoCash, which provide similar services. You may use this service for development purpose for 60 days with a trial key, for trial key check here. Let’s walk through an example, which consumes (client.php) this service.
Step 1: Check WSDL file (if any) for this service. Prepare a request for the service with ZIP code, i.e., make a SOAP envelope (use PHP NuSOAP) Step 2: Get the response (XML document) and parse it with XML parser PHP EXPAT. Step 3: Display data (Figure 3)
Listing: Client.php
<html> <head> <title> AtM INFO</title> <body> <? $xml_file = ‘az.xml’; require(‘nusoap.php’); // Prepare SOAP envelop requesting the service $wsdlfile="http://ws2.serviceobjects.net/gc/GeoCash.asmx?WSDL"; $msg='<GetATMLocations xmlns="http://www.serviceobjects.com/"><PostalCode>20191</PostalCode> <LicenseKey>WS2-BPN4-AFV4</LicenseKey></GetATMLocations>'; $s=new soapclient($wsdlfile,'wsdl'); // Invoking operation $s->call('GetATMLocations',array($msg)); // Get the response $theXML= $s->response; // To see the response, uncomment the following lines //echo ‘<xmp>’.$theXML.’</xmp>’; echo "<table border=5 cellpadding=5>"; echo "<tr><th colspan=2>ATM Info</th></tr>"; // write this response to a file for future pursing $fp = fopen ($xml_file, "w"); fwrite($fp, $theXML); fclose($fp);
//Delete 1st 8 Lines which is not a part of XML file $filename = $xml_file; $fd = fopen ($filename, "r"); $query_str="HTTP/1.1 200 OK"; $m_line=7;$tmp_line=0;$line=0; $m_filename = "result2.txt"; $m_fd = fopen($m_filename, "w"); while (!feof ($fd)) { $buffer = fgets($fd, 4096); $pos = strpos ($buffer, $query_str);
//create xml parser object $parser = xml_parser_create(); /* Define what to do when the parser encounters a start or end tag. Note that “startElement” and “endElement” are user-defined functions, which we will look at in a minute. You can name them whatever you want, but these names are the standard convention. */ xml_set_element_handler($parser,"startElement","endElement"); xml_set_character_data_handler($parser, "characterData"); if (!($filehandler = fopen($xml_file, "r"))) { die("could not open XML input"); }
/* We start reading the contents of the file, 4K at a time, and put it in the variable “$data” until we reach the end of the file. We use xml_parse to parse each chunk as we go. */
while ($data = fread($filehandler, 4096)) { if (!xml_parse($parser, $data, feof($filehandler))) { die(sprintf("XML error:%s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } }
// When the parser encounters the start element of a tag function startElement($parser_instance, $element_name, $attrs) { switch($element_name) { case "BANK" : echo "<tr><td><b><font face=verdana size=2 color=blue>"; break; case "ADDRESS" : echo "<b>"; break; case "LOCATION" : echo "<b>"; break; case "CITY" : echo "<b>"; break; case "STATE" : echo "<b>"; break; case "LATITUDE" : echo "<b>"; break; case "LONGITUDE" :echo "<b>"; break; case "NOTES" : echo "<b>"; break; } } // When the parser encounters the data within the start and end tag function characterData($parser_instance, $xml_data) { echo $xml_data; } // When the parser encounters the end element of a tag function endElement($parser_instance, $element_name) { switch($element_name) { case "BANK" : echo ", </font>"; break; case "ADDRESS" : echo ", </b>"; break; case "LOCATION" : echo ", </b>"; break; case "CITY" : echo ", </b>"; break; case "STATE" : echo " </b>"; break; case "LATITUDE" : echo ", </b>"; break; case "LONGITUDE" : echo ", </b>"; break; case "NOTES" : echo " </b><tr><td><b>"; break; } } ?>