Yada
Yada

Reputation: 31225

Need some help with XML parsing

The XML feed is located at: http://xml.betclick.com/odds_fr.xml

I need a php loop to echo the name of the match, the hour, and the bets options and the odds links. The function will select and display ONLY the matchs of the day with streaming="1" and the bets type "Ftb_Mr3".

I'm new to xpath and simplexml.

Thanks in advance.

So far I have:

<?php
$xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml");
$xml = simplexml_load_string($xml_str);

// need xpath magic
$xml->xpath();

// display

?>

Upvotes: 0

Views: 1751

Answers (3)

Calin Rusu
Calin Rusu

Reputation: 195

I am using this on a project. Scraping Beclic odds with:

<?php
        $match_csv = fopen('matches.csv', 'w');
        $bet_csv = fopen('bets.csv', 'w');
        $xml = simplexml_load_file('http://xml.cdn.betclic.com/odds_en.xml');
        $bookmaker = 'Betclick';
        foreach ($xml as $sport) {
            $sport_name = $sport->attributes()->name;
            foreach ($sport as $event) {
                $event_name = $event->attributes()->name;
                foreach ($event as $match) {
                    $match_name = $match->attributes()->name;
                    $match_id = $match->attributes()->id;
                    $match_start_date_str = str_replace('T', ' ', $match->attributes()->start_date);
                    $match_start_date = strtotime($match_start_date_str);
                    if (!empty($match->attributes()->live_id)) {
                        $match_is_live = 1;
                    } else {
                        $match_is_live = 0;
                    }
                    if ($match->attributes()->streaming == 1) {
                        $match_is_running = 1;
                    } else {
                        $match_is_running = 0;
                    }
                    $match_row = $match_id . ',' . $bookmaker . ',' . $sport_name . ',' . $event_name . ',' . $match_name . ',' . $match_start_date . ',' . $match_is_live . ',' . $match_is_running;
                    fputcsv($match_csv, explode(',', $match_row));
                    foreach ($match as $bets) {
                        foreach ($bets as $bet) {
                            $bet_name = $bet->attributes()->name;
                            foreach ($bet as $choice) {
                                // team numbers are surrounded by %, we strip them
                                $choice_name = str_replace('%', '', $choice->attributes()->name);
                                // get the float value of odss
                                $odd = (float)$choice->attributes()->odd;
                                // concat the row to be put to csv file
                                $bet_row = $match_id . ',' . $bet_name . ',' . $choice_name . ',' . $odd;
                                fputcsv($bet_csv, explode(',', $bet_row));
                            }
                        }
                    }
                }
            }
        }
        fclose($match_csv);
        fclose($bet_csv);
?>

Then loading the csv files into mysql. Running it once a minute, works great so far.

Upvotes: 0

George Sisco
George Sisco

Reputation: 621

I don't know how to work xpath really, but if you want to 'loop it', this should get you started:

<?php
$xml = simplexml_load_file("odds_fr.xml");

  foreach ($xml->children() as $child)
  {
    foreach ($child->children() as $child2)
    {
      foreach ($child2->children() as $child3)
      {
        foreach($child3->attributes() as $a => $b)
        {
          echo $a,'="',$b,"\"</br>";
        }

      }
    }
  }
?> 

That gets you to the 'match' tag which has the 'streaming' attribute. I don't really know what 'matches of the day' are, either, but...

It's basically right out of the w3c reference: http://www.w3schools.com/PHP/php_ref_simplexml.asp

Upvotes: 1

linead
linead

Reputation: 1636

Xpath is pretty simple once you get the hang of it

you basically want to get every match tag with a certain attribute

//match[@streaming=1]

will work pefectly, it gets every match tag from underneath the parent tag with the attribute streaming equal to 1

And i just realised you also want matches with a bets type of "Ftb_Mr3"

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]

This will return the bet node though, we want the match, which we know is the grandparent

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..

the two dots work like they do in file paths, and gets the match.

now to work this into your sample just change the final bit to

// need xpath magic
$nodes = $xml->xpath('//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..');

foreach($nodes as $node) {
    echo $node['name'].'<br/>';
}

to print all the match names.

Upvotes: 3

Related Questions