Re: SoapClient and arrays

From: Date: Mon, 04 Aug 2008 14:36:25 +0000
Subject: Re: SoapClient and arrays
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-277667@lists.php.net to get a copy of this message
Marten Lehmann wrote:
Hello, I'm calling a webservice that is described by a WSDL-URL using the PHP-builtin Soap client. This works fine in general. But although the response is received correct, the variable type is wrong. According to the WSDL file, the response is defined like this: <s:element name="SaveResponse"> <s:complexType>
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="SaveResult" type="s:boolean" />
      <s:element minOccurs="0" maxOccurs="1" name="key" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="messages" type="tns:ArrayOfMessage" />
    </s:sequence>
</s:complexType> </s:element> This pice of code: print gettype($response). "\n"; var_dump($response); print $response["key"]. "\n"; returns this: object object(stdClass)#2 (3) { ["SaveResult"]=> bool(true) ["key"]=> string(52) "FGLHRQXVDQJXQAWBCGJWNCQKTOYFGGMJSHYQELJPSABDGUNZWBEA" ["messages"]=> object(stdClass)#3 (0) { } } Fatal error: Cannot use object of type stdClass as array in /whatever/soap.php on line 26 You can see the structure of an associative array. But PHP doesn't treat it as an array. Now with the following code, it works fine: settype($response, "array"); print gettype($response). "\n"; var_dump($response); print $response["key"]. "\n"; This returns: array array(3) { ["SaveResult"]=> bool(true) ["key"]=> string(50) "FGLHRQXWDWKHQPWRDCKSNYRGUKZBHCNFSXZHENMTVKEHJZQPYZ" ["messages"]=> object(stdClass)#3 (0) { } } FGLHRQXWDWKHQPWRDCKSNYRGUKZBHCNFSXZHENMTVKEHJZQPYZ But it seems a bit unhandy and a workaround, if I have to the settype() thing each time I call a webservice. Is there a simpler way? Regards Marten Hi,
What about $response->key ??? What does that give? It seems to me, that $response->key will be a public property of the stdClass? -- Aschwin Wesselius /'What you would like to be done to you, do that to the other....'/

« previous php.general (#277667) next »