Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 19 ■ INTRODUCTION TO WEB SERVICES WITH SOAP^289

Introduction to SOAP


SOAP is used to exchange data between the web service and its clients. It encodes both the data
and the request. The easiest way to understand SOAP is by looking at an example of how it is
generated.
The WSDL file in Listing 19-1 defines an operation called Demo, which has one input parameter
that takes a string. If you were to call that method, the SOAP-formatted request would look like
Listing 19-2.

Listing 19-2. An Encoded SOAP Request

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/
xmlns:ns1="http://localhost/demo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
>
<SOAP-ENV:Body>
<ns1:Demo>
<param1 xsi:type="xsd:string">abcdefg</param1>
</ns1:Demo>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This message contains a SOAP envelope, which encapsulates the method call. The Envelope
element assigns the ns1 prefix to your targetNamespace in order to give it a distinct namespace. In
the body, it will call an operation using this prefix, passing along the param1 parameter defined
by the WSDL and with the data provided by the application. The PHP equivalent method call
would look like $ns1->Demo('abcdefg').
The service will then respond with a SOAP datagram that looks like Listing 19-3.

Listing 19-3. A SOAP Response

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://localhost/demo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
>

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

Free download pdf