Reputation: 73
I have a problem with processing a PHP SoapClient
response...
I know that I can access response XML elements using object notation:
$client = new SoapClient($wsdl,$options);
$data=$client->Method(array('Param'=>"Value"));
$tmp = $data->Results->Asset->Attributes>Attribute;
var_dump($tmp);
The problem is when I am trying to reach element with some attribute... Here is part of the XML resposne:
<Results>
<Asset>
<Attributes>
<Attribute Name="ID">0</Attribute>
<Attribute Name="FirstName">John</Attribute>
<Attribute Name="Size">Large</Attribute>
</Attributes>
</Asset>
<Asset>
<Attributes>
<Attribute Name="ID">1</Attribute>
<Attribute Name="FirstName">Bob</Attribute>
<Attribute Name="Size">Medium</Attribute>
</Attributes>
</Asset>
<Asset>
<Attributes>
<Attribute Name="ID">2</Attribute>
<Attribute Name="FirstName">Frank</Attribute>
<Attribute Name="Size">Small</Attribute>
</Attributes>
</Asset>
</Results>
How to get value from element "Attribute" with attribute "Size" using object notation?
In print_r
I am getting an array with stdClass
inside (it is only a part of it)
[0] => stdClass Object
(
[Attributes] => stdClass Object
(
[Attribute] => Array
(
[0] => stdClass Object
(
[_] => 0
[Name] => ID
)
[1] => stdClass Object
(
[_] => John
[Name] => FirstName
)
[2] => stdClass Object
(
[_] => Large
[Name] => Size
)
)
)
)
[1] => stdClass Object
(
[Attributes] => stdClass Object
(
[Attribute] => Array
(
[0] => stdClass Object
(
[_] => 1
[Name] => ID
)
[1] => stdClass Object
(
[_] => Bob
[Name] => FirstName
)
[2] => stdClass Object
(
[_] => Medium
[Name] => Size
)
)
)
)
[2] => stdClass Object
(
[Attributes] => stdClass Object
(
[Attribute] => Array
(
[0] => stdClass Object
(
[_] => 2
[Name] => ID
)
[1] => stdClass Object
(
[_] => Frank
[Name] => FirstName
)
[2] => stdClass Object
(
[_] => Small
[Name] => Size
)
)
)
)
I know that i can probably use
$client->__getLastResponse()
and parse with SimpleXMLElement
but it does not seem optimal for me...
Upvotes: 1
Views: 635
Reputation: 42701
You should be able to access it at $data->Results->Asset->Attributes->Attribute[0]->_
.
If you want to search for your value where the attribute is "size" then you will probably need a loop. It's hard to tell what your data looks like with such a small sample, but I don't believe more concise methods like array_column()
will work with that structure.
<?php
foreach ($data->Results->Asset as $Asset) {
foreach ($Asset->Attributes->Attribute as $Attribute) {
if ($Attribute->Name === "Size") {
echo $Attribute->_;
}
}
}
Upvotes: 1