WSDL Tutorials - Herong's Tutorial Examples - v2.22, by Herong Yang
Calling an RPC Method Based Web Service
This section provides a tutorial example on how to write a PHP program to call an RPC method based Web service using the SoapClient class.
Ok, let's try to use the SoapClient class to call my RPC Web service, GetExchangeRate_WSDL_11_SOAP_11_RPC.wsdl, first.
Here is the example PHP program, GetExchangeRate_Client.php:
<?php # GetExchangeRate_Client.php # Copyright (c) 2007 HerongYang.com. All Rights Reserved. # #- Loading the WSDL document $server = "https://www.herongyang.com/Service/"; $wsdl = $server . "GetExchangeRate_WSDL_11_SOAP_11_RPC.wsdl"; $client = new SoapClient($wsdl); #- Calling the RPC method $result = $client->GetExchangeRate('USD', 'JPY', '2007-07-07'); #- Showing the result print "Exchange rate is: $result\n"; ?>
Execution result of this example is presented below:
herong> \local\php\php GetExchangeRate_Client.php PHP Fatal error: SOAP-ERROR: Parsing WSDL: Unspecified encodingStyle in C:\herong\GetExchangeRate_Client.php on line 8 ... Stack trace: #0 C:\herong\GetExchangeRate_Client.php(8): SoapClient->SoapClient('http://www.hero...') #1 {main} thrown in C:\herong\GetExchangeRate_Client.php on line 8
What's wrong here? The error message says, encodingStyle is not specified. Let's review the WSDL document:
<wsdl:binding name="getExchangeRateBinding" type="hy:getExchangeRatePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetExchangeRate"> <soap:operation style="rpc" soapAction="https://www.herongyang.com/Service/getExchangeRate"/> <wsdl:input name="getExchangeRateInput"> <soap:body use="encoded" parts="fromCurrencyPart toCurrencyPart datePart"/> </wsdl:input> <wsdl:output name="getExchangeRateOutput"> <soap:body use="encoded" parts="ratePart"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
The root cause of the problem is in <soap body ...> elements. I did not provide the encodingStyle="uri" attribute. The error above tells us that the SoapClient class needs encodingStyle="uri" if use="encoded" is used.
To fix the WSDL document is easy, just add encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" in <soap body ...> elements. But I am not going to fix it and keep it as is for testing purpose.
Conclusion, SoapClient class requires encodingStyle="uri", if use="encoded" is used. SoapClient class does not provide any default value to encodingStyle.
Table of Contents
WSDL 2.0 Document Structure and Syntax
WSDL Version 2.0 Part 2: Adjuncts
WSDL 2.0 Document Examples with SOAP Binding
Using WSDL Document in Java Apache Axis2/Java for WSDL
Apache Woden for WSDL Documents in Java
SoapUI - Web Service Testing Tool
WSDL 1.1 Document Structure and Syntax
WSDL 1.1 Binding Extension for SOAP 1.1
SoapUI as WSDL 1.1 Testing Tool
WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles
►PHP SOAP Extension for WSDL 1.1
Testing SOAP Extension with WSDL 1.1
Methods on the SoapClient Class
►Calling an RPC Method Based Web Service
encodingStyle="uri" Required for rpc/encoded
SoapParam Constructor - Creating Named Parameters
SoapVar Constructor - Creating Encoded Values
XSD_ANYXML Encoding - Building SOAP Body Element
Calling an XML Document Based Web Service
Apache Axis2/Java for WSDL 1.1
Using WSDL2Java to Generate Web Service Stub Classes
WSDL 1.1 Binding Extension for SOAP 1.2
WSDL 1.1 and SOAP 1.2 Examples - Document and RPC Styles