SoapParam Constructor - Creating Named Parameters

This section provides a tutorial example on how to create input parameters with given names when calling Web services with SoapClient.

In the previous tutorial, I provided input parameters with values to allow the SoapClient add names based on the WSDL document:

   $client->GetStockPrice('GOOG', 'NASDAQ', '2007-07-07');

What should I do if I want to control parameter names myself? The SOAP Extension manual mentions the SoapParam class constructor to create named parameters:

   $param = new SoapParam($value, $name);

So let me try it with tutorial program, SoapClient_SoapParam.php:

<?php
# SoapClient_SoapParam.php
# Copyright (c) 2007 HerongYang.com. All Rights Reserved.
#
#- Loading the WSDL document
   $server = "https://www.herongyang.com/service/";
   $wsdl = $server . "GetStockPrice_WSDL_11_SOAP_11_RPC1.wsdl";
   $client = new SoapClient($wsdl,
      array('trace' => TRUE));

#- Constructing parameters
   $stock = new SoapParam('GOOG', 'Stock');
   $market = new SoapParam('NASDAQ', 'Market');
   $date = new SoapParam('2007-07-07', 'Date');

#- Calling the RPC method
   $result = $client->GetStockPrice($stock, $market, $date);

#- Showing the result
   print "Stock price is: $result\n";

#- Showing the request
   print $client->__getLastRequest()."\n";
?>

Here is the result of this tutorial program:

Stock price is: 552.16

<SOAP-ENV:Envelope ...>
  <SOAP-ENV:Body>
    <SOAP-ENV:GetStockPrice>
      <stockPart xsi:type="xsd:string">GOOG</stockPart>
      <marketPart xsi:type="xsd:string">NASDAQ</marketPart>
      <datePart xsi:type="xsd:date">2007-07-07</datePart>
    </SOAP-ENV:GetStockPrice>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Surprisingly, SoapClient ignored my parameter names, and continued to use input message part names defined in the WSDL document.

Maybe SoapClient will use parameter names provided by SoapParam if input message parts are not defined with names.

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

 Using WSDL Document in Java Apache Axis2/Java for WSDL

 Apache Woden for WSDL Documents in Java

 SoapUI - Web Service Testing Tool

 PHP SOAP Extension for WSDL

 Perl SOAP::Lite for WSDL

 Introduction to WSDL 1.1

 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

 Perl SOAP::Lite for WSDL 1.1

 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

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 Python SOAP Client: Zeep

 WSDL Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB