21,886
社区成员
发帖
与我相关
我的任务
分享
$src = 'http://f.apiplus.cn/cqssc.xml';
$xml = file_get_contents(urldecode($src));
$xml = simplexml_load_string($xml);
foreach($xml->row as $r) {
foreach($r->attributes() as $k=>$v) {
if($k == "expect") $v = join("-", str_split($v, 8));
echo "$k => $v\n";
}
}
expect => 20170606-040
opencode => 9,6,8,6,0
opentime => 2017-06-06 12:40:40
expect => 20170606-039
opencode => 3,5,7,8,1
opentime => 2017-06-06 12:30:40
expect => 20170606-038
opencode => 6,8,6,7,3
opentime => 2017-06-06 12:20:40
expect => 20170606-037
opencode => 5,9,8,2,4
opentime => 2017-06-06 12:10:40
expect => 20170606-036
opencode => 8,9,3,4,4
opentime => 2017-06-06 12:00:40
echo substr('2070605032',0,8).'-'.substr('2070605032',-3,3);//20706050-032
$src = 'http://f.apiplus.cn/cqssc.xml';
$src .= '?_='.time();
$xml = file_get_contents(urldecode($src));
$xml = simplexml_load_string($xml);
$data = array();
for ($i = 0,$count = count($xml->row); $i < $count; $i++) {
$p = $xml->row[$i]->attributes()->expect;
$tmp = array(
'expect' => substr($p,0,8).'-'.substr($p,-3,3),//开奖期号
'opencode' => current($xml->row[$i]->attributes()->opencode),//开奖号码
'opentime' => current($xml->row[$i]->attributes()->opentime),//开奖时间
);
$data[] = $tmp;
}
$json = json_encode($data);
echo 'json 格式<br>';
echo $json;
echo '<hr>';
echo '数组格式:';
echo "<pre>";
print_r(json_decode($json,true));
echo "</pre>";
/*
json 格式
[{"expect":"20170606-060","opencode":"4,3,6,7,2","opentime":"2017-06-06 16:00:40"},{"expect":"20170606-059","opencode":"1,3,6,6,4","opentime":"2017-06-06 15:50:40"},{"expect":"20170606-058","opencode":"9,1,7,4,9","opentime":"2017-06-06 15:40:40"},{"expect":"20170606-057","opencode":"4,5,6,0,1","opentime":"2017-06-06 15:30:40"},{"expect":"20170606-056","opencode":"1,8,0,1,3","opentime":"2017-06-06 15:20:40"}]
数组格式:
Array
(
[0] => Array
(
[expect] => 20170606-060
[opencode] => 4,3,6,7,2
[opentime] => 2017-06-06 16:00:40
)
[1] => Array
(
[expect] => 20170606-059
[opencode] => 1,3,6,6,4
[opentime] => 2017-06-06 15:50:40
)
[2] => Array
(
[expect] => 20170606-058
[opencode] => 9,1,7,4,9
[opentime] => 2017-06-06 15:40:40
)
[3] => Array
(
[expect] => 20170606-057
[opencode] => 4,5,6,0,1
[opentime] => 2017-06-06 15:30:40
)
[4] => Array
(
[expect] => 20170606-056
[opencode] => 1,8,0,1,3
[opentime] => 2017-06-06 15:20:40
)
)
*/
<?php
$url="http://f.apiplus.cn/cqssc.xml";
$con=file_get_contents($url);
$con=simpalexml_load_string($con);
foreach($con as $r){
foreach($r->attribute() as $k=>$v){
if($k=="expect"){
$v=join("-",str_split($v,8));
}
echo $k.$v."<br>";
}
}
?>
采集地址:http://f.apiplus.cn/cqssc.xml
2017-06-06 13:35:12共采集到5行开奖数据:
开奖期号:20170606-043
开奖号码:9,8,5,5,9
开奖时间:2017-06-06 13:10:40
开奖期号:20170606-042
开奖号码:9,0,8,1,4
开奖时间:2017-06-06 13:00:40
开奖期号:20170606-041
开奖号码:0,5,3,5,7
开奖时间:2017-06-06 12:50:40
开奖期号:20170606-040
开奖号码:9,6,8,6,0
开奖时间:2017-06-06 12:40:40
开奖期号:20170606-039
开奖号码:3,5,7,8,1
开奖时间:2017-06-06 12:30:40
有什么不对吗?