SoapClient and arrays
| From: | Marten Lehmann | Date: | Mon, 04 Aug 2008 13:31:11 +0000 |
| Subject: | SoapClient and arrays | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-277666@lists.php.net to get a copy of this message | ||
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