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) 2009 by Dr. Herong Yang, herongyang.com
# All rights reserved

#- Loading the WSDL document
   $server = "http://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:

C:\herong\>\local\php\php GetExchangeRate_Client.php

PHP Fatal error:  SOAP-ERROR: Parsing WSDL: Unspecified encodingStyle
   in C:\herong\GetExchangeRate_Client.php on line 9
...
Stack trace:
#0 C:\herong\GetExchangeRate_Client.php(9): 
   SoapClient->SoapClient('http://www.hero...')
#1 {main} thrown in C:\herong\GetExchangeRate_Client.php on line 9

What's wrong here? The error message says, encodingStyle is not specified. Let's review the WSDL docuement:

  <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="http://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.

Last update: 2009.

Table of Contents

 About This Book

 Introduction to WSDL 2.0

 WSDL 2.0 Document Structure and Syntax

 WSDL Version 2.0 Part 2: Adjuncts

 WSDL 2.0 Document Examples with SOAP Binding

 WSDL 20 Programming APIs and Testing Tools

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 soapUI 3.0.1 - Web Service Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

PHP SOAP Extension in PHP 5.3.1

 What Is PHP SOAP Extension?

 Downloading and Installing PHP 5.3.1

 Configuring and Testing SOAP Extension

 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

 Using WSDL in Perl with SOAP::Lite 0.710

 Using WSDL Document in Java with Axis2 1.4.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

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 WSDL Related Terminologies

 References

 PDF Printing Version