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

马特·哈奇(Matt Hutch)

我正在使用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
    },
  ]
}
查尔斯·帕尔默

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

        $recent = $data->recentMatches;

这是一个对象数组。所以

        echo $recent[1]->score;

会显示“ 865”,这是示例中列出的第二场比赛的得分。

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章