Consuming a Web Service with PHP 5's SOAP ExtensionConsumption 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 (wsdl-php5-client.php)<?php $soap = new SoapClient('AddService.wsdl'); try { $result = $soap->add(47, 11); echo "47 + 11 = $result"; } catch (SoapFault $e) { echo "Error: {$e->faultstring}"; } }
Using the Worst Acronym Ever: AJAXWikipedia 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 phrase of the book shows a very short demonstration of "AJAX." This phrase 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! Figure 9.5 shows the result. The client-side part of the application (ajax.php; excerpt)<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 ></div> Figure 9.5. The text appears, using JavaScript and PHP.
|