I am writing a script that grabs an XML string from a webserver and then tries to parse it. I am using SimpleXML to parse the XML and am running into a problem directly accessing the elements of the array and object.
Here is the code with var_dumps:
print "------------------------------- Report Request Response\n";
$post_data = array( "seq" => "67890" );
$conn = curl_init( $LIST_REPORTS_URL );
$dumb = main_curl ();
$result = curl_exec( $conn );
$xml = new SimpleXMLElement( $result );
curl_close( $conn );
$contents = $xml->contents;
$reports = $xml->contents->reports;
$report_array = (array) $reports->report;
$count = count ( $report_array );;
print "Number of Reports: $count\n";
foreach ($report_array as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
print "------------------------------- XML var_dump\n";
var_dump($xml);
print "------------------------------- Reports var_dump\n";
var_dump($reports);
print "------------------------------- Report Array var_dump\n";
var_dump($report_array);
And here is the output from the code:
------------------------------- Report Request Response
Number of Reports: 4
Key: name; Value: 704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756<br />
Key: status; Value: completed<br />
Key: readableName; Value: LAN<br />
Key: timestamp; Value: 1385189528<br />
------------------------------- XML var_dump
object(SimpleXMLElement)#2 (3) {
["seq"]=>
string(5) "67890"
["status"]=>
string(2) "OK"
["contents"]=>
object(SimpleXMLElement)#10 (1) {
["reports"]=>
object(SimpleXMLElement)#12 (1) {
["report"]=>
array(3) {
[0]=>
object(SimpleXMLElement)#13 (4) {
["name"]=>
string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
["status"]=>
string(9) "completed"
["readableName"]=>
string(3) "LAN"
["timestamp"]=>
string(10) "1385189528"
}
[1]=>
object(SimpleXMLElement)#14 (4) {
["name"]=>
string(52) "237d4645-c39a-fb55-6c78-d1654ff312fa220b62510846ef58"
["status"]=>
string(9) "completed"
["readableName"]=>
string(7) "BOTScan"
["timestamp"]=>
string(10) "1385224078"
}
[2]=>
object(SimpleXMLElement)#15 (4) {
["name"]=>
string(52) "ded003bd-fb8d-c06f-08d5-3c9af762a0d32e4e1930c3833a5d"
["status"]=>
string(9) "completed"
["readableName"]=>
string(8) "BOTScan1"
["timestamp"]=>
string(10) "1385224768"
}
}
}
}
}
------------------------------- Reports var_dump
object(SimpleXMLElement)#11 (1) {
["report"]=>
array(3) {
[0]=>
object(SimpleXMLElement)#10 (4) {
["name"]=>
string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
["status"]=>
string(9) "completed"
["readableName"]=>
string(3) "LAN"
["timestamp"]=>
string(10) "1385189528"
}
[1]=>
object(SimpleXMLElement)#12 (4) {
["name"]=>
string(52) "237d4645-c39a-fb55-6c78-d1654ff312fa220b62510846ef58"
["status"]=>
string(9) "completed"
["readableName"]=>
string(7) "BOTScan"
["timestamp"]=>
string(10) "1385224078"
}
[2]=>
object(SimpleXMLElement)#15 (4) {
["name"]=>
string(52) "ded003bd-fb8d-c06f-08d5-3c9af762a0d32e4e1930c3833a5d"
["status"]=>
string(9) "completed"
["readableName"]=>
string(8) "BOTScan1"
["timestamp"]=>
string(10) "1385224768"
}
}
}
------------------------------- Report Array var_dump
array(4) {
["name"]=>
string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
["status"]=>
string(9) "completed"
["readableName"]=>
string(3) "LAN"
["timestamp"]=>
string(10) "1385189528"
}
I am trying to figure out how to directly address each element in the report array, but can only access the first one. (See the foreach section above). Wheter I use (array) or not in $result_array, I still get the correct number of records in the array, I just can’t get the individual array records to come out.
Thanks for the help!
Lorell