25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

210 ” XML and Web Services<br />

}<br />

When creating a SOAP server with SoapServer, you must decide whether your server<br />

will operate in WSDL or non-WSDL mode. At present, SoapServer will not automatically<br />

generate a WSDL file based on an existing <strong>PHP</strong> class, although this feature is<br />

planned for a future release. For now, you can either create your WSDL files manually—usually<br />

an incredibly tedious task, use a tool (like the Zend Studio IDE) that will<br />

generate one for you, or choose not to provide one at all. For the sake of simplicity,<br />

our example SOAP server will operate in non-WSDL mode.<br />

Once we have created the server, we need to inform it of the class that we want the<br />

web service to be based on. In this case, our SOAP server will use the MySoapServer<br />

class. Finally, to process incoming requests, call the handle() method:<br />

$options = array(’uri’ => ’http://example.org/soap/server/’);<br />

$server = new SoapServer(NULL, $options);<br />

$server->setClass(’MySoapServer’);<br />

$server->handle();<br />

While this SOAP service will work just fine in non-WSDL mode, it is important to<br />

note that a WSDL file can be helpful to both users of the service and to the SoapServer<br />

object itself. For users, a WSDL file helps expose the various methods and data types<br />

available. For the server, the WSDL file allows the mapping of different WSDL types<br />

to <strong>PHP</strong> classes, thus making handling complex data simpler.<br />

The following example shows how a client might access the SOAP server described<br />

in this section. Notice how the client is able to access the getMessage() and<br />

addNumbers() methods of the MySoapServer class:<br />

$options = array(<br />

’location’ => ’http://example.org/soap/server/server.php’,<br />

’uri’ => ’http://example.org/soap/server/’<br />

);<br />

$client = new SoapClient(NULL, $options);<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

echo $client->getMessage() . "\n";<br />

echo $client->addNumbers(3, 5) . "\n";

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!