Home arrow PHP arrow Page 4 - Building XML Web Services with PHP NuSOAP
PHP

Building XML Web Services with PHP NuSOAP


In this article, Ahm shows us how to build XML Web Services using PHP NuSOAP, as an alternative to Microsoft .NET or Sun J2EE.

Author Info:
By: Ahm Asaduzzaman
Rating: 5 stars5 stars5 stars5 stars5 stars / 78
February 06, 2003
TABLE OF CONTENTS:
  1. · Building XML Web Services with PHP NuSOAP
  2. · What are Web Services?
  3. · Walkthrough Example 1
  4. · Walkthrough Example 2
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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
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);

  if ($pos === false and $line==0)
  {
       fputs ($m_fd,$buffer);
  }
else
  {
     if ($tmp_line < $m_line)
     {
       $tmp_line = $tmp_line + 1;
       $line = 1;
    }
     else
     {
      $line = 0;
     }
  }
}
fclose($fd);
fclose($m_fd);
copy("result2.txt","result.txt");
$xml_file=$m_filename;

//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)));
  }
}

fclose($filehandler); xml_parser_free($parser);
echo "</table>";

?>
</font>
</body>
</html>


<?php

// Functions

// 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;
  }
}
?>

blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials