PHP
  Home arrow PHP arrow Page 4 - Building XML Web Services with PHP NuSOAP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building XML Web Services with PHP NuSOAP
By: Ahm Asaduzzaman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 52
    2003-02-06

    Table of Contents:
  • Building XML Web Services with PHP NuSOAP
  • What are Web Services?
  • Walkthrough Example 1
  • Walkthrough Example 2
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


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

    More PHP Articles
    More By Ahm Asaduzzaman


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT