日期:2014-05-17  浏览次数:20499 次

php 取xml中的数据 --超级简单---急急
数据源:http://www.caipiaokong.com/api/lottery/xml.php?type=ssq
要取它的前3行数据

------解决方案--------------------
$url = 'http://www.caipiaokong.com/api/lottery/xml.php?type=ssq';
$ar = file($url);
print_r(array_slice($ar, 2, 3));
Array
(
    [0] =>  <row expect="13113" opencode="04,07,11,17,24,33
------解决方案--------------------
09" opentime="2013-09-26 21:30:00" ballsnum="1" />

    [1] =>  <row expect="13112" opencode="01,06,12,13,22,31
------解决方案--------------------
07" opentime="2013-09-24 21:30:00" ballsnum="1" />

    [2] =>  <row expect="13111" opencode="01,02,03,06,08,33
------解决方案--------------------
13" opentime="2013-09-22 21:30:00" ballsnum="1" />

)
$url = 'http://www.caipiaokong.com/api/lottery/xml.php?type=ssq';
$xml = simplexml_load_file($url);
foreach($xml->row as $i=>$v) {
  if($i > 2) break;
  $res[] = current((array)$v->attributes());
  
}
print_r($res);
Array
(
    [0] => Array
        (
            [expect] => 13113
            [opencode] => 04,07,11,17,24,33
------解决方案--------------------
09
            [opentime] => 2013-09-26 21:30:00
            [ballsnum] => 1
        )

    [1] => Array
        (
            [expect] => 13112
            [opencode] => 01,06,12,13,22,31
------解决方案--------------------
07
            [opentime] => 2013-09-24 21:30:00
            [ballsnum] => 1
        )

    [2] => Array
        (
            [expect] => 13111
            [opencode] => 01,02,03,06,08,33
------解决方案--------------------
13
            [opentime] => 2013-09-22 21:30:00