Parsing external XML stream in php

Im really new to php and im trying to load data from an external XML stream into a php document and then use that data to generate output.

Using the XML im file - http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=1&marketSort=--&filterBIR=N

What I'm trying to do is create a list of "markets" and names there, since the xml feed is at the time of writing the first three items in the list:

  • Scottish Division 1 - Straight - Straight
  • Dumbarton in Hamilton 1st Half Result / 2nd Half Result
  • Dumbarton to Hamilton - Handicap Match

at the moment when I try to use the below code to achieve this, but I am not getting anywhere from it, any ideas on what I am doing wrong here?

just another piece of background, im using php 5.4.4 i understand correctly that simplexml comes pre-installed already. So I don't need to add any additional stuff 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;
}

?>

      

+3


source to share


2 answers


You need to scroll through the xml page to get the markets and then get the attributes on 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>";
}

      

+2


source


You can, for example, show the name of the participants and the corresponding odds:



<?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 />";
}

?>

      

0


source







All Articles