Reputation: 10094
Im really new to php and im trying to load in data from an external xml feed into a php document, then use that data to generate an output.
The xml feed im using is - http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N
What im trying to do is generate a list of 'markets' and there names, so as the xml feed stands at the time of writing the first 3 items in the list would be :
at the moment im trying to use the code bellow to achieve this, but im getting nowhere quickly with it, any ideas on what im doing wrong here ?
just a further piece of background, im using php 5.4.4, am i right in thinking that simplexml comes already pre installed.. so i dont need to add any thing additional here ?
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
foreach ($xml->market as $event) {
echo $event;
}
?>
Upvotes: 3
Views: 9180
Reputation: 846
You need to drill down through the xml to get the markets, and then get the attributes of the market:
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
foreach ($xml->response->williamhill->class->type as $type) {
$type_attrib = $type->attributes();
echo "<p><h2>Type ".$type_attrib['id'].": ".$type_attrib['name']."</h2>";
foreach ($type->market as $event) {
$event_attributes = $event->attributes();
echo $event_attributes['name']."<br />";
//commented out the following which prints all attributes
//replaced by above to just print the name
/*
echo "<p>";
foreach($event->attributes() as $attrib=>$value) {
echo "$attrib: $value <br />";
}
echo "</p>";
*/
}
echo "</p>";
}
Upvotes: 3
Reputation: 3500
You could for example show the name of participants and respective odds doing this:
<?php
$xml = simplexml_load_file('http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N');
$data = $xml->response->williamhill->class->type->market;
$ps = $data->participant;
foreach($ps as $p)
{
echo $p['name']." - ".$p['odds']."<br />";
}
?>
Upvotes: 0