jeudi 13 août 2015

update multiple json item based in ids

I'm trying to do some work with php and json, so.. i'm doing and live score app, basically i write the data that come from the api to a json file, i have two different methods to get data from the api, one gives me the full list of games for an entire day, for example today, i can request this method every 5 minutes, what is too long for an live score app, the second method only show me the live games and i can request every 25 seconds, that's cool for me, so what i do right now is:

  • I request the 5 minutes method in the first minutes of every day to get the full list, and write the data to a file with the day date.

the json file for every day looks like this

{
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"
}

The data for live games that i can request every 25 seconds it's like the json above but with a few more items like , Time (game time), hometeamgoals, awayteamgoals , so what i want to do is request data every 25 seconds to this live method and compare the match id and update with time, hometamgoals, awaysteamgoals all the games that are live.

Correct me if you found a better way ... Right now i have a live.php file that is requesting data for live games, and store that info in a live.json file, after that i want to compare the data in the live.json with the full list of games for today and update the one that match the live.json.

My code in 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);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire