PHP JSON Steam API 请求并打印

PHP JSON Steam API request and print

我的代码有什么问题?我想打印 total_time_played 和更多详细信息。

$url = 'http://api.steampowered.com/ISteamUserStats/GetUserStatsForGame/v0002/?appid=730&key=7BBE9B2D821CEBBC0F4944912BE26DC5&steamid=76561197980138287';
$urljson = file_get_contents('$url');


$data = json_decode($urljson)->playerstats;

$new_array = array();
foreach ($data->stats as $item) {
    $new_array[$item->name] = $item->value;
}
echo $new_array['total_time_played'];

在这里您将尝试从文字 '$url' 字符串中获取内容,而不是 $url 变量的字符串值。

这个:

$urljson = file_get_contents('$url');

需要改成这样:

$urljson = file_get_contents($url);

请注意单引号(防止变量插值)已被删除。