Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^290) CHAPTER 19 ■ INTRODUCTION TO WEB SERVICES WITH SOAP





Request received with param1 = abcdefg




The function that generated this response looks like Listing 19-4.
Listing 19-4. A Demo Function
function Demo($param1) {
return 'Request received with param1 = '. $param1;
}

Using the PHP SOAP Extension


Now that you have some understanding of WSDL and SOAP, it is easy to explain how to use the
PHP SOAP extension to create and interact with web services.
The SoapServer class allows you to publish a web service using PHP. Using this class, you
add functions that will fulfill the defined operations in your WSDL file. Listing 19-5 shows a sample
SOAP server.

Listing 19-5. A Complete SOAP Server (service.php)

<?php

function Demo($param1) {
return 'Request received with param1 = '. $param1;
}

$server = new SoapServer('demo.wsdl');
$server->addFunction('Demo');
$server->handle();

The SoapClient class allows you to remotely call web services—invoke an operation, pass
parameters, and receive a response. This class will handle two key tasks: generating a SOAP
envelope and sending it to the remote service that you just created. Listing 19-6 shows a sample
SOAP client.

Listing 19-6. A Sample SOAP Client (client.php)

<?php
$client = new SoapClient('demo.wsdl');
$response = $client->Demo('abcdefg');
echo $response;

McArthur_819-9.book Page 290 Friday, February 29, 2008 8:03 AM

Free download pdf