我正在尝试用php和json做一些工作,所以.. 我正在做和得分应用程序,基本上我把来自api的数据写入json文件,我有两种不同的方法从api获取数据,一个给我一整天的游戏完整列表,今天的例子,我可以每5分钟请求一次这个方法,对于一个实时分数应用程序来说太长了,第二种方法只显示我的实时游戏,我可以每25秒请求一次,这对我来说很酷,所以我现在做的是什么是:
每天的json文件看起来像这样
{
Match: [
{
Id: "353516",
Date: "2015-08-14T19:30:00+00:00",
League: "Primeira Liga",
Round: "1",
HomeTeam: "Tondela",
HomeTeam_Id: "1304",
AwayTeam: "Sporting",
AwayTeam_Id: "539",
Location: "Estádio João Cardoso",
LeagueFlag: "Portugal.png",
PhotoAds: "no ads",
AwayTeamShort: "SCP",
HomeTeamShort: "TON"
},
{
Id: "356795",
Date: "2015-08-14T19:00:00+00:00",
League: "Süper Lig",
Round: "1",
HomeTeam: "Fenerbahce",
HomeTeam_Id: "223",
AwayTeam: "Eskisehirspor",
AwayTeam_Id: "209",
Location: "Şükrü Saracoğlu Stadium",
LeagueFlag: "Turkey.png",
PhotoAds: "no ads"
}
实时游戏的数据,我可以每25秒请求一次,就像上面的json,但还有一些项目,如时间(游戏时间),hometeamgoals,awayteamgoals,所以我想做的是每25秒请求一次数据这个实时方法比较匹配ID和更新时间,hometamgoals,awaysteamgoals所有现场比赛。
如果你找到了更好的方法,请纠正我...... 现在我有一个live.php文件,它正在为实时游戏请求数据,并将该信息存储在live.json文件中,之后我想将live.json中的数据与今天的完整游戏列表进行比较。更新与live.json匹配的那个。
我在live.php中的代码:
$result=$soccer->GetLiveScore();
$fp = fopen( "/var/www/live.json","w+");
fwrite($fp,json_encode($result));
fclose($fp);
$datacinco = "/var/www/live.json";
$json = file_get_contents($datacinco);
$links = json_decode($json, TRUE);
foreach($links["Match"] as $key=>$val) {
//right now i'm not doing anything here, it's right here that i need your help
}
$fp = fopen( "/var/www/$filedate.json","w+"); // i write the info checked above in a file with the day date where is the full list of games
fwrite($fp,json_encode($links));
fclose($fp);
25秒响应(它是相同但有更多数据):
{
Match: [
{
Id: "348358",
Date: "2015-08-13T22:30:00+00:00",
League: "Brasileirao",
Round: "18",
Spectators: { },
HomeTeam: "Ponte Preta",
HomeTeam_Id: "745",
AwayTeam: "Avai FC",
AwayTeam_Id: "1203",
Time: "Finished",
HomeGoals: "2",
AwayGoals: "0",
HomeGoalDetails: "73': Felipe Azevedo;60': Borges;",
AwayGoalDetails: { },
HomeLineupGoalkeeper: "Marcelo Lomba",
AwayLineupGoalkeeper: "Diego",
HomeLineupDefense: "Renato Chaves; Pablo; Gilson; Rodinei;",
AwayLineupDefense: "Jeci; Emerson; Romario; Nino Paraiba;",
HomeLineupMidfield: "Fernando Bob; Bady; Josimar;",
AwayLineupMidfield: "Tinga; Pablo; Nestor Abraham Camacho; Renan Oliveira; Adriano;",
HomeLineupForward: "Felipe Azevedo; Cesinha; Borges;",
AwayLineupForward: "Roberto;",
HomeLineupSubstitutes: "Joao Carlos; Felipe; Tiago Alves; Jefferson; Diego Oliveira; Elton; Leandrinho; Juninho; Keno;",
AwayLineupSubstitutes: "Juninho; Toshi; Everton Silva; Eduardo Neto; Hugo; Marrone; Iury; Vagner; Conrado;",
HomeLineupCoach: { },
AwayLineupCoach: { },
HomeSubDetails: "68': out Bady;68': in Juninho;65': out Borges;65': in Diego Oliveira;54': in Keno;54': out Cesinha;",
AwaySubDetails: "74': in Conrado;74': out Adriano;62': out Nestor Abraham Camacho;62': in Hugo;55': out Roberto;55': in Iury;",
HomeTeamFormation: "4-3-3",
AwayTeamFormation: "4-5-1",
Location: "Estádio Moisés Lucarelli",
Stadium: "Estádio Moisés Lucarelli",
HomeTeamYellowCardDetails: { },
AwayTeamYellowCardDetails: { },
HomeTeamRedCardDetails: { },
AwayTeamRedCardDetails: { }
}