3

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 :

  • Scottish Division 1 - Outright - Outright
  • Dumbarton v Hamilton - 1st Half Result/2nd Half Result
  • Dumbarton v Hamilton - Match Handicaps

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;
}

?>
3
  • What is the problem? It is working right here. Try to print_r($xml); Commented Jan 21, 2013 at 16:26
  • @IvoPereira im managing to get the whole feed using print_r($xml); but im having trouble picking it the <market id="39970574" name="Scottish Division 1 - Outright - Outright" part out using the foreach loop Commented Jan 21, 2013 at 16:29
  • Hi @sam, I need the WSDL for this old api (since v2 is protected by an ApiKey that I cannot manage to get). Do you have the wsdl by any chance?? Many thanks Commented Oct 28, 2018 at 5:56

2 Answers 2

3

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>";
}
Sign up to request clarification or add additional context in comments.

4 Comments

By the way, while you are testing you should save the xml file to a local file and just load it from there, it will be a lot quicker and save bandwidth.
thanks bencoder, this works to format the output of the xml data, but it outputs every attribute given, rather than just the name
Hi @sam, I changed it so it will only print the name part. I commented out the part which prints all the attributes so you can compare to help you understand
thanks very much for that, it really helps to be able to pick it apart and see what each bit does. Thanks again
0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.