SOAP Web Service Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
HelloServerWsdl2.php - SOAP 1.1/1.2 Server in WSDL Mode
This section describes a tutorial example of a complete SOAP application with both server and client programs that supports both SOAP 1.1 and SOAP 1.2 with a single WSDL file.
You can actually create a single server application to support both SOAP 1.1 and SOAP 1.2 in WSDL mode. Here is the same hello server modified to work in WSDL mode for SOAP 1.1/SOAP 1.2, HelloServerWdsl2.php:
<?php # HelloServerWsdl2.php # Copyright (c) 2009 HerongYang.com. All Rights Reserved. # function hello($someone) { return "Hello " . $someone . "! - With WSDL/SOAP 1.1/1.2"; } ini_set("soap.wsdl_cache_enabled", "0"); $server = new SoapServer("http://localhost/Hello2.wsdl"); $server->addFunction("hello"); $server->handle(); ?>
Nothing special in the program. The server object is now created with the location of the WSDL document. The SOAP version will be determined dynamically based on the SOAP request message.
Here is the WSDL document, Hello2.wsdl, which supports both SOAP 1.1 and SOAP 1.2:
<?xml version="1.0"?> <!-- Hello2.wsdl - Copyright (c) 2009 HerongYang.com. All Rights Reserved. --> <definitions name="MyDefinition" targetNamespace="urn:myTargetNamespace" xmlns:tns="urn:myTns" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <message name="myRequest"> <part name="reqParam" type="xsd:string"/> </message> <message name="myResponse"> <part name="resParam" type="xsd:string"/> </message> <portType name="MyPortType"> <operation name="hello"> <input message="tns:myRequest"/> <output message="tns:myResponse"/> </operation> </portType> <binding name="MyBinding" type="tns:MyPortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="hello"> <soap:operation soapAction=""/> <input> <soap:body use="encoded" namespace="urn:myInputNamespace" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body use="encoded" namespace="urn:myOutputNamespace" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <binding name="MyBinding12" type="tns:MyPortType"> <soap12:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="hello"> <soap12:operation soapAction=""/> <input> <soap12:body use="encoded" namespace="urn:myInputNamespace" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/> </input> <output> <soap12:body use="encoded" namespace="urn:myOutputNamespace" encodingStyle="http://www.w3.org/2003/05/soap-encoding"/> </output> </operation> </binding> <service name="MyService"> <documentation>Returns a greeting string. </documentation> <port name="MyPort" binding="tns:MyBinding"> <soap:address location="http://localhost/HelloServerWsdl2.php"/> </port> <port name="MyPort12" binding="tns:MyBinding12"> <soap12:address location="http://localhost/HelloServerWsdl2.php"/> </port> </service> </definitions>
To install HelloServerWsdl2.php to my Apache server, copy these two files to the Apache document directory:
herong> copy HelloServerWsdl2.php \local\apache\htdocs herong> copy Hello2.wsdl \local\apache\htdocs
Of course, we need to modify our hello client program as, HelloClientWsdl2.php:
<?php # HelloClientWsdl2.php # Copyright (c) 2009 HerongYang.com. All Rights Reserved. # $soap_version = '1.1'; if (count($argv) > 1) $soap_version = $argv[1]; if ($soap_version == '1.1') { $client = new SoapClient("http://localhost/Hello2.wsdl", array('soap_version' => SOAP_1_1, 'trace' => 1 )); } else if ($soap_version == '1.2') { $client = new SoapClient("http://localhost/Hello2.wsdl", array('soap_version' => SOAP_1_2, 'trace' => 1 )); } else { $client = new SoapClient("http://localhost/Hello2.wsdl", array('trace' => 1 )); } echo("\nDumping client object functions:\n"); var_dump($client->__getFunctions()); $return = $client->__soapCall("hello",array("world")); echo("\nReturning value of __soapCall() call: ".$return); # $return = $client->hello("world"); # echo("\nReturning value: ".$return); echo("\nDumping request headers:\n" .$client->__getLastRequestHeaders()); echo("\nDumping request:\n".$client->__getLastRequest()); echo("\nDumping response headers:\n" .$client->__getLastResponseHeaders()); echo("\nDumping response:\n".$client->__getLastResponse()); ?>
Now run "HelloClientWsdl2.php 1.1" to test the SOAP 1.1 version:
herong> php HelloClientWsdl2.php 1.1 ... Dumping response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:myOutputNamespace" 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:helloResponse> <resParam xsi:type="xsd:string">Hello world! - With WSDL/SOAP 1.1/1.2</resParam> </ns1:helloResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
Good. I am getting the response back in SOAP 1.1 format as expected.
Then run "HelloClientWsdl2.php 1.2" to test the SOAP 1.2 version:
herong> php HelloClientWsdl2.php 1.2 Dumping response: <?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:myOutputNamespace" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:enc="http://www.w3.org/2003/05/soap-encoding"> <env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc"> <ns1:helloResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"> <rpc:result>resParam</rpc:result> <resParam xsi:type="xsd:string">Hello world! - With WSDL/SOAP 1.1/1.2</resParam> </ns1:helloResponse> </env:Body> </env:Envelope>
Cool. I am getting the response back in SOAP 1.2 format as expected.
Table of Contents
Introduction to SOAP (Simple Object Access Protocol)
SOAP Message Transmission and Processing
SOAP MEP (Message Exchange Patterns)
PHP SOAP Extension Client Programs
►PHP SOAP Extension Server Programs
PHP SOAP Extension Functions for Server Programs
HelloServer.php - First SOAP Server Application
HelloServer12.php - SOAP 1.2 Server Application
HelloServerWsdl.php - SOAP 1.2 Server in WSDL Mode
►HelloServerWsdl2.php - SOAP 1.1/1.2 Server in WSDL Mode
PHP SOAP Web Service Example - getTemp
Perl SOAP::Lite - SOAP Server-Client Communication Module
Perl Socket Test Program for HTTP and SOAP
Perl SOAP::Lite for NumberToWords SOAP 1.1 Web Service
Perl SOAP::Lite for SOAP 1.2 Web Services
Java Socket and HttpURLConnection for SOAP
SAAJ - SOAP with Attachments API for Java
SoapUI - SOAP Web Service Testing Tool
WS-Security - SOAP Message Security Extension
WS-Security X.509 Certificate Token
Perl SOAP::Lite for GetSpeech SOAP 1.1 Web Service
Perl SOAP::Lite 0.710 for SOAP 1.2 Web Services
Perl SOAP::Lite 0.710 for WSDL