Tuesday, 17 September 2013

PHP - how to read this XML file

PHP - how to read this XML file

I have the following xml:
<CallOverview>
<Calls Count="2">
<Call CallType="LandLine" Customer="this account"
StartTime="2013-09-17 20:21:19 (UTC)" Destination="NL"
Duration="00:00:05" Charge="0.045" CallId="158263673"/>
<Call CallType="Mobile" Customer="this account"
StartTime="2013-09-17 20:18:34 (UTC)" Destination="US"
Duration="00:00:26" Charge="0.101" CallId="158263381"/>
</Calls>
<MoreData>No more data available</MoreData>
</CallOverview>
I retrieve the XML like this:
function get_xml($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
};
I receive the XML as desired, but I can't manage to extract the data the
right way:
$xml=simplexml_load_string ( get_xml($url_post) ) ;
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
foreach($child->children() as $subchild)
{
echo $subchild->getName() . ": " . $subchild . "<br>";
$role = $child->attributes();
foreach($child as $key => $value) {
echo "$role $key $value";
}
}
}

No comments:

Post a Comment