Creating a Web Service with PHP


$soap = new SoapServer(   'RandomNumberService.wsdl',   array('uri' =>     'http://javascript.phrasebook.org/') ); 

So that the Web Service can be used, we first need one. The most popular server-side technology at the moment is PHP (PHP: Hypertext Preprocessor), so this is the obvious choice for implementing a Web Service. For this to work, you need a web server with PHP installed. Many affordable web hosting packages come with PHP nowadays, and PHP can also be installed on most types of web server. You can download the PHP distribution and source from its homepage, http://www.php.net/.

For this to work, you will need PHP 5 or later, and you have to enable the SOAP library that comes with it. On UNIX/Linux, you have to compile PHP with the configuration switch --enable-soap; users of Windows have to add the following line to the php.ini configuration file:

extension=php_soap.dll 


Also, you need a WSDL filethat's a description for the Web Service, including which methods it exposes and which parameters it expects. The file RandomNumberService.wsdl contains all this information. Make sure that you look for the section <soap:address location="http://localhost/js/webservice.php" /> of the WSDL and adapt the URL to your local system.

Then, the following script implements a simple SOAP Web Service with one method (that returns a random number within a given interval):

A PHP Web Service (webservice.php)

<?php   $soap = new SoapServer(     'RandomNumberService.wsdl',     array('uri' =>       'http://javascript.phrasebook.org/')   );   $soap->setClass('ServiceClass');   $soap->handle();   class ServiceClass {     function randomNumber($lower, $upper) {       return rand($lower, $upper);     }   } ?> 




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net