Hi,
I'm calling a web service in PHP and am having difficulties dealing with the response, in terms of how to read from the XML I get returned.
I create the SOAP client object and call the web service like this:
$client = new SoapClient('http://blahblah/eventservice.asmx?wsdl',
array(
'trace' => 1,
'login' => $username,
'password' => $password));
$response = $client->Sports();
If I do a print_r of the response, here is the message returned by the web service:
print_r($response);
stdClass Object
(
[Result] => stdClass Object
(
[any] => <sports expirydate="2010-01-13T11:09:00"><sport ID="1" desc="Football"/><sport ID="2" desc="American Football"/><sport ID="3" desc="Basketball"/>
...
...
</sports>
)
)
From this, I assumed that I got an XML object back and can get to the data items using '->' but although it partly works, I can't get to the <sport> elements and it is driving me a bit mad.
If I try this:
$xml = $response->Result;
print_r ($xml);
...then it gives me the contents of the 'Result' element, as I would expect, like this:
stdClass Object
(
[any] => <sports expirydate="2010-01-13T11:09:00"><sport ID="1" desc="Football"/><sport ID="2" desc="American Football"/><sport ID="3" desc="Basketball"/>
...
...
</sports>
)
But how do I access the <sport> elements? What does [any] mean?
What I am trying to do is print a list of the 'desc' attributes, i.e. 'Basketball' but whatever I try does not work.
$sports = $xml->sports; // Does not work: Undefined property: stdClass::$sports
$sport = $xml->sport[1]; // Does not work: Undefined property: stdClass::$sport
$sport = $xml->sports->sport[1]; //Undefined property: stdClass::$sports, Trying to get property of non-object
I also tried to use the SimpleXML library like this but it fails:
$xml = new SimpleXMLElement($response->Result);
// Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects parameter 1 to be string, object given'
What I would like to do is something like this:
// Does not work:
// Undefined property: stdClass::$sports
// Invalid argument supplied for foreach()
foreach ($response->Result->sports as $sport)
echo "ID" . $sport['ID'] . " description is " . $sport['desc'];
I'm sure I am doing something really dumb here but can't figure it out. Any comments or suggestions gratefully received! Thank you.