首先,我从unibet.com获得了一个xml feed,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<betOfferResponse>
<events>
<name>Red Star Belgrade - CSKA Moscow</name>
<homeName>Red Star Belgrade</homeName>
<awayName>CSKA Moscow</awayName>
<start>2018-02-13T17:00Z</start>
<group>Europa League</group>
<type>ET_MATCH</type>
<sport>FOOTBALL</sport>
<state>NOT_STARTED</state>
<liveBetOffers>true</liveBetOffers>
<openForLiveBetting>false</openForLiveBetting>
<id>1004394826</id>
<groupId>2000051195</groupId>
<path>
<id>1000093190</id>
<name>Football</name>
<englishName>Football</englishName>
</path>
<path>
<id>2000051195</id>
<name>Europa League</name>
<englishName>Europa League</englishName>
</path>
</events>
<events>
<name>FC Astana - Sporting Lisbon</name>
<homeName>FC Astana</homeName>
<awayName>Sporting Lisbon</awayName>
<start>2018-02-15T16:00Z</start>
<group>Europa League</group>
<type>ET_MATCH</type>
<sport>FOOTBALL</sport>
<state>NOT_STARTED</state>
<liveBetOffers>true</liveBetOffers>
<openForLiveBetting>false</openForLiveBetting>
<id>1004394832</id>
<groupId>2000051195</groupId>
<path>
<id>1000093190</id>
<name>Football</name>
<englishName>Football</englishName>
</path>
<path>
<id>2000051195</id>
<name>Europa League</name>
<englishName>Europa League</englishName>
</path>
</events>
但是当我使用我的脚本读取并将xml数据放入我的数据库时,它不起作用,因为这些:
<betOfferResponse>
但是当我创建自己的xml而只有下面的数据时,它可以工作。但是它无法循环文件并获得第二个条目,依此类推......
这是我的PHP代码:
<?php
$xml=simplexml_load_file("test.xml") or die("Error: Cannot create object");
$connection = mysqli_connect("localhost", "root", "password", "tipstr") or die ("ERROR: Cannot connect");
/* Assumes that the number of IDs = number of customers */
$size = sizeOf($xml->id);
$i = 0; //index
/* Add each customer to the database, See how we reference it as $xml->ENTITY[INDEX] */
while($i != $size)
{
print_r($xml);
echo $xml->id[$i]; //Test
$sql = "INSERT INTO xf_nflj_sportsbook_event (event_id, category_id, user_id, username, title, description, event_status, date_create, date_open, date_close, date_settle, date_edit, event_timezone, wagers_placed, amount_staked, amount_paidout, likes, like_users, view_count, outcome_count, comment_count, thread_id, prefix_id, last_comment_date, limit_wagers_single_outcome) VALUES ('$xml->id',2,1,'tipstr', '$xml->name', '$xml->sport', 'open', 1517755596,1517755596,1517761200,1517761200,0,'Europe/London', 0, 0,0,0,0,0,0,0,0,0,0,0)";
mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");
$i++; //increment index
}
mysqli_close($connection);
我做错了什么?我如何循环/ foreach插入查询,以便它可以获取整个文件,而不仅仅是第一个,以及如何让它从unibet读取xml?
编辑: 很抱歉没有添加我成功时使用的xml。但这是:
<events>
<name>Marseille - S.C. Braga</name>
<homeName>Marseille</homeName>
<awayName>S.C. Braga</awayName>
<start>2018-02-15T18:00Z</start>
<group>Europa League</group>
<type>ET_MATCH</type>
<sport>FOOTBALL</sport>
<state>NOT_STARTED</state>
<liveBetOffers>true</liveBetOffers>
<openForLiveBetting>false</openForLiveBetting>
<id>1004394835</id>
<groupId>2000051195</groupId>
<path>
<id>1000093190</id>
<name>Football</name>
<englishName>Football</englishName>
</path>
<path>
<id>2000051195</id>
<name>Europa League</name>
<englishName>Europa League</englishName>
</path>
</events>
<betoffers>
<id>2086105579</id>
<eventId>1004044027</eventId>
<closed>2018-05-16T18:45Z</closed>
<live>false</live>
<startingPrice>false</startingPrice>
<criterion>
<id>1001221607</id>
<label>Winner</label>
</criterion>
<betOfferType>
<id>4</id>
<name>Winner</name>
</betOfferType>
<outcomes>
<id>2307553711</id>
<odds>15000</odds>
<label>AC Milan</label>
<type>OT_UNTYPED</type>
<changedDate>2018-02-05T22:19:04Z</changedDate>
<oddsFractional>14/1</oddsFractional>
<oddsAmerican>1400</oddsAmerican>
</outcomes>
<outcomes>
<id>2307553715</id>
<odds>7000</odds>
<label>Arsenal</label>
<type>OT_UNTYPED</type>
<changedDate>2018-02-05T22:19:04Z</changedDate>
<oddsFractional>6/1</oddsFractional>
<oddsAmerican>600</oddsAmerican>
</outcomes>
我没有得到任何特殊错误,当我在xml中添加更多项目时,只写了我写的php错误。所以尝试检查consol中的网络选项卡,但也没有。
答案 0 :(得分:0)
您应该更改循环以使其更容易......
WSLENV
请注意,我已经更改了数据嵌入字符串的方式,因为现在每个元素都被调用<?php
$xml=simplexml_load_file("xml url") or die("Error: Cannot create object");
$connection = mysqli_connect("localhost", "root", "password", "db_name") or die ("ERROR: Cannot connect");
foreach ( $xml->events as $event ) {
$sql = "INSERT INTO xf_nflj_sportsbook_event
(event_id, category_id, user_id, username, title,
description, event_status, date_create, date_open,
date_close, date_settle, date_edit, event_timezone,
wagers_placed, amount_staked, amount_paidout, likes,
like_users, view_count, outcome_count, comment_count, thread_id,
prefix_id, last_comment_date, limit_wagers_single_outcome)
VALUES ('{$event->id}',2,1,'tipstr', '{$event->name}', '{$event->sport}',
'open',
1517755596,1517755596,1517761200,1517761200,0,'Europe/London',
0, 0,0,0,0,0,0,0,0,0,0,0)";
mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");
}
我已将它们更改为$event
等。
您还应该查看预准备语句,因为您可以准备一次该语句,然后对每行数据执行多次。
这不产生任何输出,只是从文件中将数据插入数据库。
答案 1 :(得分:0)
感谢Nigel Ren给我正确的公式,这里有正确的答案和所有缺失的数据..
现在我终于可以从unibet的xml中获取数据了:)
<?php
$xml=simplexml_load_file("xml url") or die("Error: Cannot create object");
$connection = mysqli_connect("localhost", "root", "password", "db_name") or die ("ERROR: Cannot connect");
/* Assumes that the number of IDs = number of customers */
$size = sizeOf($xml->id);
$i = 2; //index
/* Add each customer to the database, See how we reference it as $xml->ENTITY[INDEX] */
while($i != $size)
{
print_r($xml);
echo $xml->id[$i]; //Test
foreach($xml->events as $event) //Extract the Array Values by using Foreach Loop
{
$sql = "INSERT INTO xf_nflj_sportsbook_event (event_id, category_id, user_id, username, title, description, event_status, date_create, date_open, date_close, date_settle, date_edit, event_timezone, wagers_placed, amount_staked, amount_paidout, likes, like_users, view_count, outcome_count, comment_count, thread_id, prefix_id, last_comment_date, limit_wagers_single_outcome) VALUES ('$event->id',2,1,'tipstr', '$event->name', '$event->sport', 'pending', 1517755596,1517755596,1517761200,1517761200,0,'Europe/London', 0, 0,0,0,0,0,0,0,0,0,0,0)";
mysqli_query($connection, $sql) or die ("ERROR: " .mysqli_error($connection) . " (query was $sql)");
$i++; //increment index
}
}
mysqli_close($connection);