从Fortnite Tracker API中的JSON文件中提取数据

时间:2018-04-18 09:01:38

标签: php json

我正在使用Fortnite Tracker API从我的网站获取一些数据,我已经连接它,它将把我当前的数据导出到JSON文件中。这已经很好了,因为我使用了几个统计数据,但由于某些原因我不能让它与最近的比赛一起工作,有人能看到我在哪里出错吗?

PHP

<?php

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/psn/mygamertag");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_HEADER, FALSE);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'TRN-Api-Key: myapikey'
  ));
  $response = curl_exec($ch);
  curl_close($ch);
  $fp = fopen("myStats.json", "w");
  fwrite($fp, $response);
  fclose($fp);

  $data = json_decode(file_get_contents("myStats.json"));
  $solo = $data->stats->p2;//solos data
  $duos = $data->stats->p10;//duos data
  $squads = $data->stats->p9;//squads data

  $matches = $data->recentMatches;//match data
  $sesh1 = $matches[0]->id->valueInt;

  $solo_wins = $solo->top1->valueInt;
  $duos_wins = $duos->top1->valueInt;
  $squads_wins = $squads->top1->valueInt;
  $solo_matches = $solo->matches->valueInt;
  $duos_matches = $duos->matches->valueInt;
  $squads_matches = $squads->matches->valueInt;
  $solo_kd = $solo->kd->valueDec;
  $duos_kd = $duos->kd->valueDec;
  $squads_kd = $squads->kd->valueDec;
  $solo_games = $solo->matches->valueInt;
  $duos_games = $duos->matches->valueInt;
  $squads_games = $squads->matches->valueInt;
  $solo_kills = $solo->kills->valueInt;
  $duos_kills = $duos->kills->valueInt;
  $squads_kills = $squads->kills->valueInt;

  $total_matches = ($solo_matches+$duos_matches+$squads_matches);

  $total_wins = ($solo_wins+$duos_wins+$squads_wins);

  $total_kills = ($solo_kills+$duos_kills+$squads_kills);

  $total_kd = (round($total_kills/($total_matches-$total_wins),2));

  echo 'Total Matches: '.$total_matches.'<br>';
  echo 'Total Wins: '.$total_wins.'<br>';
  echo 'Total Kills: '.$total_kills.'<br>';
  echo 'Total KD: '.$total_kd.'<br>';
  echo $sesh1;

?>

JSON文件 - Solo(这适用于PHP)

{
  "stats": {
    "p2": {
      "score": {
        "label": "Score",
        "field": "Score",
        "category": "General",
        "valueInt": 31960,
        "value": "31960",
        "rank": 3045650,
        "percentile": 47.0,
        "displayValue": "31,960"
      },
      "top1": {
        "label": "Wins",
        "field": "Top1",
        "category": "Tops",
        "valueInt": 4,
        "value": "4",
        "rank": 1904807,
        "percentile": 42.0,
        "displayValue": "4"
      },
      "top3": {
        "label": "Top 3",
        "field": "Top3",
        "category": "Tops",
        "valueInt": 0,
        "value": "0",
        "rank": 1,
        "displayValue": "0"
      },
      "top10": {
        "label": "Top 10",
        "field": "Top10",
        "category": "Tops",
        "valueInt": 44,
        "value": "44",
        "rank": 1923906,
        "percentile": 9.0,
        "displayValue": "44"
      },
      "top25": {
        "label": "Top 25",
        "field": "Top25",
        "category": "Tops",
        "valueInt": 79,
        "value": "79",
        "rank": 2519315,
        "percentile": 11.0,
        "displayValue": "79"
      },
      "kd": {
        "label": "K/d",
        "field": "KD",
        "category": "General",
        "valueDec": 0.71,
        "value": "0.71",
        "rank": 5155051,
        "percentile": 63.0,
        "displayValue": "0.71"
      },
      "matches": {
        "label": "Matches",
        "field": "Matches",
        "category": "General",
        "valueInt": 127,
        "value": "127",
        "rank": 4270142,
        "percentile": 59.0,
        "displayValue": "127"
      },
      "kills": {
        "label": "Kills",
        "field": "Kills",
        "category": "General",
        "valueInt": 87,
        "value": "87",
        "rank": 4376486,
        "percentile": 58.0,
        "displayValue": "87"
      },
    }
  ]
}

JSON文件 - 最近的匹配(努力在PHP中提取数据)

{
  "recentMatches": [
    {
      "id": 149942602,
      "accountId": "9e9e2e92-870c-4596-a4a0-1276b16d2b2e",
      "playlist": "p9",
      "kills": 1,
      "minutesPlayed": 0,
      "top1": 0,
      "top5": 0,
      "top6": 0,
      "top10": 0,
      "top12": 0,
      "top25": 0,
      "matches": 1,
      "top3": 0,
      "dateCollected": "2018-04-18T08:09:28.963",
      "score": 290,
      "platform": 2,
      "trnRating": 767.6,
      "trnRatingChange": -103.23
    },
    {
      "id": 148934320,
      "accountId": "9e9e2e92-870c-4596-a4a0-1276b16d2b2e",
      "playlist": "p9",
      "kills": 1,
      "minutesPlayed": 0,
      "top1": 1,
      "top5": 0,
      "top6": 1,
      "top10": 0,
      "top12": 0,
      "top25": 0,
      "matches": 3,
      "top3": 1,
      "dateCollected": "2018-04-17T22:19:04.747",
      "score": 865,
      "platform": 2,
      "trnRating": 870.8,
      "trnRatingChange": -100.7124
    },
    {
      "id": 147725308,
      "accountId": "9e9e2e92-870c-4596-a4a0-1276b16d2b2e",
      "playlist": "p9",
      "kills": 0,
      "minutesPlayed": 0,
      "top1": 0,
      "top5": 0,
      "top6": 0,
      "top10": 0,
      "top12": 0,
      "top25": 0,
      "matches": 2,
      "top3": 0,
      "dateCollected": "2018-04-17T14:22:34.047",
      "score": 423,
      "platform": 2,
      "trnRating": 906.0,
      "trnRatingChange": -99.85293750000001
    },
  ]
}

1 个答案:

答案 0 :(得分:1)

作为json的新手,我遇到了同样的问题。为了解决这个问题,我为最近的比赛创建了一个容器。

        $recent = $data->recentMatches;

这是一个对象数组。所以

        echo $recent[1]->score;

将打印&#39; 865&#39;,这是您示例中列出的第二场比赛的得分。

希望这有帮助。