PHP-Fortnite API向我显示“无效的身份验证凭据”?

时间:2018-07-16 12:00:17

标签: php api

我想制作一个小的Fortnite API,但是在JSON文件中总是会出现错误。

{"message":"Invalid authentication credentials"}

我的PHP代码:     

$ch = curl_init();
//pc, xbl, psn
curl_setopt($ch, CURLOPT_URL, "https://api.fortnitetracker.com/v1/profile/pc/MyName");
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: My-API-Code'
));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
$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;

?>

我输入了正确的API代码。为什么此消息写在JSON文件中而不是我的胜利?太疯狂了,因为它可以正常工作。

1 个答案:

答案 0 :(得分:0)

第11行返回什么?您正在使用var_dump($response); 您能在该行下写die();并提供给我们它的回报吗?

您处理标头的方式可能有问题。由于我不在家,我现在无法确定。但是我也许以后可以测试一下。