PHP

Consuming a Web Service with PHP 5's SOAP Extension

Consumption is again easy, as long as you have a WSDL description. This code calls the Web Services and also catches any errors, thanks to PHP 5's TRy-catch mechanism.

Consuming the Web Service with PHP5-SOAP

<?php
  $soap = new SoapClient('AddService.wsdl');
  try {
    $result = $soap->add(47, 11);
    echo "47 + 11 = $result";
  } catch (SoapFault $e) {
    echo "Error: {$e->faultstring}";
  }
}

In an attempt to boost performance, PHP 5's SOAP extension defaults to caching WSDL. The following are the standard settings from php.ini-recommended:

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

When developing a Web Service and maybe changing the WSDL, this is, of course, a no-brainer. Therefore, set soap.wsdl_cache_enabled to Off during development, but turn it on on production servers.

Using the Worst Acronym Ever: AJAX

Wikipedia lists over a dozen different meanings for the term "Ajax," including a soccer team from the Netherlands, two figures of Homer's Iliad, and a household-cleaning product. However, since several months ago, AJAX also stands for "Asynchronous JavaScript + XML." The technology is far from new (the underlying technology, XML HTTP requests from JavaScript, are supported by recent versions of Internet Explorer, and Mozilla browsers including Firefox, Safari, and Konqueroralso, XML is not required at all for this), but only after this really stupid term was coined, did people actually start using it.

The basic principle is that JavaScript is now able to call a remote server and then process its return values, without having to reload the whole page. Of course, this has little to do with PHP; however, some AJAX classes make using AJAX from within PHP very easy.

This final of the tutorial shows a very short demonstration of "AJAX." This uses the Sajax toolkit (the S stands for simple) available at http://www.modernmethod.com/sajax/.

The complete code for this example is quite long because both PHP and JavaScript are needed. First, we need the server-side logic. For this, we the Sajax toolkit is loaded, which comes as a single PHP file. We then "export" (register) all PHP functions we want to use from the client-side. The following example has a small function that generates a random RGB color.

The server-side part of the application (ajax.php; excerpt)
<?php
  require_once 'Sajax.php';
  sajax_init();
  sajax_export('randomColor');
  sajax_handle_client_request();
  function randomColor() {
    $color = '#';
    for ($i=0; $i<3; $i++) {
      $color .= dechex(rand(0, 255));
    }
    return $color;
  }
?>

On the client-side, a call to the (PHP) function sajax_show_javascript() does most of the workit creates some lines of JavaScript code that take care of the requests to the web server in the background. Then, all PHP functions that have been registered before can be called on the client-side by prepending x_ to the name of the function. As a parameter, you have to provide the name of a callback function; this function is then called when the server-side has responded to the request.

The following code calls the randomColor() PHP function every five seconds and then writes a text in this random colorall without server round-trips! The figure shows the result.

The client-side part of the application
<script language="JavaScript" ype="text/javascript"
  ><!--
<?php
  sajax_show_javascript();
?>
  changeColor();
  setInterval('changeColor()', 5000);
  function changeColor() {
    x_randomColor(randomColor_callback);
  }
  function randomColor_callback(result) {
    var text = '<span style="color: ' + result +
      '">What a dumb acronym ...</span>';
    document.getElementById('output').innerHTML =
      text;
  }
//--></script>
...
<div id="output"></div>

The text appears, using JavaScript and PHP.

What Does PEAR Offer?

Along with the several modules mentioned in this chapter, the following PEAR packages offer functionality that can be helpful when connecting with others using PHP:

  • The HTTP category of PEAR offers some relevant packages, including HTTP, HTTP_Client, HTTP_Header, HTTP_Request, and HTTP_Server.

  • Likewise, you can find useful packages in the Networking category, including Net_FTP, Net_IMAP, and Net_Socket.

  • The Services category of PEAR contains several classes that offer an easy to use interface for well-known Web Services.

by BrainBellupdated
Advertisement: