Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 20 ■ ADVANCED WEB SERVICES^309

ini_set('soap.wsdl_cache_enabled', '0');
$server = new SoapServer('demo.wsdl');
$server->setClass("Service");
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();

Upon the first request to the service, the SoapServer class creates a session for the SoapClient.
It then initializes the class and uses it to complete the requested operation. Then, instead of
just discarding the class instance as it normally would, SoapServer stores the class instance in
the client’s session. When the next request is made, instead of initializing the class again, it
uses the instance stored in the user’s session.
This approach will improve performance if you have any significant construction code,
and it can also eliminate the need to use session variables directly.

Binary Data Transmission


To transmit binary data, like images or video, to your web service, it must first be encoded
into a text representation. For this purpose, web services use base64 encoding and the
base64binary XML Schema type.
Let’s walk through an example that uses an image file. This service provides an AddPhoto
operation that will allow you to send an image file from your client to your server. The key
elements in this example are the base64binary data type and the base64 encoding of the file
contents. Listing 20-10 shows the WSDL file for the example.

Listing 20-10. A WSDL File for Transmitting Binary Data (photo.wsdl)

<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Photo'
targetNamespace='http://localhost/Photo'
xmlns:tns=' http://localhost/Photo'
xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns='http://schemas.xmlsoap.org/wsdl/'>

<message name='AddPhotoRequest'>
<part name='image' type='xsd:base64binary'/>
</message>

<message name='AddPhotoResponse'>
<part name='Result' type='xsd:string'/>
</message>

<portType name='PhotoPortType'>
<operation name='AddPhoto'>
<input message='tns:AddPhotoRequest'/>

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

Free download pdf