Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP/Soap not possible to return a complex type or array??

burdeen
Guest
 
Posts: n/a
#1: Jul 17 '05
I've been trying to return an array with PHP5 soap. Is this not
supported? or am i doing it wrong? Seems to return on the last element
in the array. In my WSDL i've defined my return type as a complex
type:

<s:complexContent><s:restriction base="SOAP-ENC:Array"><s:attribute
ref="SOAP-ENC:arrayType"
wsdl:arrayType="xsd:string[]"/></s:complexContent>

Actual Output :

object(stdClass)#2 (1) { ["item"]=> object(stdClass)#3 (2) {
["key"]=> string(1) "c" ["value"]=> string(1) "3" } }

Expected:

"a" => "1" and "b" => "2" elements are missing, but "c" => "3" is
there.



Client.php ===
<?php
$client = new SoapClient("http://localhost:84/server.wsdl");
$test = $client->getList();
print(var_dump($test));
?>

Server.php ====
<?php
function getList() {
return array("a" => 1,"b" => 2,"c" => 3); // the array we're
returning
}
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("server.wsdl");
$server->addFunction("getList");
$server->handle();
?>

server.wsdl ===
<?xml version="1.0" encoding="UTF-8"?>
<y:definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:s0="http://10.1.1.151:84/soap/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:y="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns="http://10.1.1.151:84/soap/"
targetNamespace="http://10.1.1.151:84/soap/">
<y:types>
<s:schema targetNameSpace="http://10.1.1.151:84/soap/"
elementFormDefault="qualified">
<s:complexType name="getListArray">
<s:complexContent>
<s:restriction base="SOAP-ENC:Array">
<s:attribute ref="SOAP-ENC:arrayType"
wsdl:arrayType="xsd:string[]"/>
</s:restriction>
</s:complexContent>
</s:complexType>
</s:schema>
</y:types>
<y:message name="getListSoapOut">
<part name="parameter" type="y:getListArray"/>
</y:message>
<portType name="SoapPortType">
<operation name="getList">
<output message="s0:getListSoapOut"/>
</operation>
</portType>
<y:binding name="SoapBinding" type="s0:SoapPortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<y:operation name="getList">
<soap:operation soapAction="urn:#getList" style="rpc"/>
<output>
<soap:body use="literal"/>
</output>
</y:operation>
</y:binding>
<service name="SoapService">
<port name="SoapPort" binding="s0:SoapBinding">
<soap:address location="http://10.1.1.151:84/server.php?wsdl"/>
</port>
</service>
</y:definitions>

burdeen
Guest
 
Posts: n/a
#2: Jul 17 '05

re: PHP/Soap not possible to return a complex type or array??


burdeen wrote in message news:<1b62743a.0410280834.3c99ab79@posting.google. com>...[color=blue]
> I've been trying to return an array with PHP5 soap. Is this not
> supported? or am i doing it wrong? Seems to return on the last element
> in the array. In my WSDL i've defined my return type as a complex
> type:
>[/color]

So, I've solved my own problem... I believe it was the fault of the
WSDL, maybe PHP/Soap is sensitive to my weird namespaces. The thing
that helped the most was the PHP/Soap unit tests; i was able to write
a wsdl that was able to get the results i wanted. take a look here:
http://cvs.php.net/pecl/soap/tests/schema

Hopefully this will help someone else. There is very little (or no?)
documents yet on how to return complex types from the server... so
here is an example. Server will return an array of strings to the
client as SOAP-ENC:arrayType string[].


client2.php ===

<?php
$client = new SoapClient("http://localhost:84/server2.wsdl",
array("trace"=>1,"exceptions"=>1));
$test = $client->test();
var_dump($test);
?>


server2.php ===

<?php

function test() {
return array("ham","cheese","bacon");
}

ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$server = new SoapServer("server2.wsdl");
$server->addFunction("test");
$server->handle();

?>


server2.wsdl ===

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns="http://test-uri/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:ns="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:ns1="http://schemas.xmlsoap.org/wsdl/mime/"
targetNamespace="http://test-uri/" name="InteropTest">
<wsdl:types>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://test-uri/">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/"/>
<complexType name="testType">
<complexContent>
<restriction base="SOAP-ENC:Array">
<attribute ref="SOAP-ENC:arrayType" wsdl:arrayType="string[]"/>
</restriction>
</complexContent>
</complexType>
</schema>
</wsdl:types>
<message name="testMessage">
<part name="testParam" type="tns:testType"/>
</message>
<portType name="testPortType">
<operation name="test">
<output message="tns:testMessage"/>
</operation>
</portType>
<binding name="testBinding" type="tns:testPortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="test">
<soap:operation soapAction="test" style="document"/>
<output>
<soap:body parts="body" use="literal"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://test-uri/"/>
</output>
</operation>
</binding>
<service name="testService">
<port name="testPort" binding="tns:testBinding">
<soap:address location="http://localhost:84/server2.php?wsdl"/>
</port>
</service>
</wsdl:definitions>
Janwillem Borleffs
Guest
 
Posts: n/a
#3: Jul 17 '05

re: PHP/Soap not possible to return a complex type or array??


burdeen wrote:[color=blue]
> Hopefully this will help someone else. There is very little (or no?)
> documents yet on how to return complex types from the server... so
> here is an example. Server will return an array of strings to the
> client as SOAP-ENC:arrayType string[].
>[/color]

Good research, perhaps you could add an example to the
http://www.php.net/soap documentation page.


JW



Closed Thread