SoapVar Constructor - Creating Encoded Values

This section provides a tutorial example on how to create input parameters with type encoded values. Built-in types are XSD_STRING, XSD_BOOLEAN, XSD_DECIMAL, XSD_DATE, XSD_ANYXML, etc.

In the previous tutorial, I tried to specify input parameter names with the SoapParam constructor. It did not work because my WSDL document has parameter names explicitly defined.

In this tutorial, I want test the SoapVar constructor, which can be used to encode strings into SOAP values:

   $value = new SoapVar($string, encoding);

"encoding" should be one of predefined constants like: XSD_STRING, XSD_BOOLEAN, XSD_DECIMAL, XSD_DATETIME, XSD_TIME, XSD_DATE, XSD_ANYURI, XSD_ANYXML, etc.

Here is my testing tutorial program, SoapClient_SoapVar.php:

<?php 
# SoapClient_SoapVar.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 . "GetStockPrice_WSDL_11_SOAP_11_RPC.wsdl";
   $client = new SoapClient($wsdl,
      array('trace' => TRUE));

#- Test 1: Calling with values directly
   $result = $client->GetStockPrice('GOOG', 'NASDAQ', '2007-07-07');
   print $client->__getLastRequest()."\n";

#- Test 2: Calling with SoapVal objects correct encoding types
   $stock = new SoapVar("GOOG", XSD_STRING);
   $market = new SoapVar("NASDAQ", XSD_STRING);
   $date = new SoapVar("2007-07-07", XSD_DATE);
   $result = $client->GetStockPrice($stock, $market, $date);
   print $client->__getLastRequest()."\n";

#- Test 3: Calling with SoapVal objects with incorrect encoding types
   $stock = new SoapVar("<Stock>GOOG</Stock>", XSD_STRING);
   $market = new SoapVar("<Market>NASDAQ</Market>", XSD_ANYXML);
   $date = new SoapVar("2007-07-07", XSD_DATETIME);
   $result = $client->GetStockPrice($stock, $market, $date);
   print $client->__getLastRequest()."\n";
?>

Here is the result of this tutorial program:

<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>

<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>

<SOAP-ENV:Envelope ...">
  <SOAP-ENV:Body>
    <SOAP-ENV:GetStockPrice>
      <stockPart xsi:type="xsd:string">&lt;Stock&gt;GOOG
        &lt;/Stock&gt;</stockPart>
      <Market>NASDAQ</Market>
      <datePart xsi:type="xsd:dateTime">2007-07-07</datePart>
    </SOAP-ENV:GetStockPrice>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

This result is very interesting:

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