Pregunta

La fuente XML se encuentra en: http://xml.betclick.com/odds_fr.xml

Necesito un bucle php a hacerse eco del nombre del partido, la hora, y las opciones de apuestas y las probabilidades enlaces. La función será seleccionar y mostrar sólo los matchs del día con el streaming = "1" y las apuestas tipo "Ftb_Mr3".

Soy nuevo en XPath y simplexml.

Gracias de antemano.

Hasta ahora tengo:

<?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

?>
¿Fue útil?

Solución

XPath es bastante simple una vez que el cuelgue de ella

que, básicamente, quiere conseguir cada etiqueta partido con un cierto atributo

//match[@streaming=1]

funcionará pefectly, se hace cada etiqueta partido de debajo de la etiqueta principal con el atributo de transmisión igual a 1

Y me di cuenta de que también quiere Corresponde a un tipo de apuestas "Ftb_Mr3"

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

Esto devolverá el nodo apuesta sin embargo, queremos que el partido, que sabemos que es el abuelo

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

los dos puntos funcionan como lo hacen en las rutas de archivos, y se pone el partido.

Ahora a trabajar esto en su muestra simplemente cambiar el bit final a

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

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

para imprimir todos los nombres de los partidos.

Otros consejos

No sé cómo funciona realmente XPath, pero si quieres 'bucle', esto debería empezar:

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

      }
    }
  }
?> 

Eso te lleva a la etiqueta 'partido' que tiene el atributo 'streaming'. Realmente no sé qué 'partidos de la jornada' son, tampoco, pero ...

Se trata básicamente de la derecha de la referencia del W3C: http://www.w3schools.com/PHP/php_ref_simplexml.asp

Estoy usando esto en un proyecto. Raspando probabilidades Beclic con:

<?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);
?>

A continuación, cargar los archivos CSV en MySQL. Ejecutarlo una vez por minuto, funciona muy bien hasta ahora.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top