我正在尝试运行此PHP文件,并且我一直收到错误,如
Undefined property: stdClass::$games
和
Invalid argument supplied for foreach()
这是错误http://imageshack.us/a/img845/6270/34356212.jpg的图片,下面是我正在运行的脚本减去我的Steam API密钥。
<?php
$api_key = '........'; //Steam API Key, Required to work
$lines = file('parse1.txt'); //reads in the file with the list of user numbers
$game_id = 550; //Steam Game ID, 440 is TF2
foreach ($lines as $usernumber) { //loops line by line
$link = 'http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=' . $api_key . '&steamid=' . $usernumber . '&format=json';
$json = file_get_contents($link); //Reads link (this ^)
$data = json_decode($json);
foreach ($data->response->games as $game) {
if ($game->appid == $game_id) {
$playtime = $game->playtime_forever;
if (($playtime < 5400) && ($playtime > 1)) {
//echo 'Playtime for ', $usernumber, ' is ' . $playtime."<br>";
echo $usernumber."<br>";
}
}
}
}
?>
Parse.txt包含蒸汽ID,例如(76561197987683795,76561198063283029) 我认为它与嵌套的IF命令有关,但我不知道我缺少什么,任何帮助都会很大!感谢
答案 0 :(得分:2)
json中的response
对象不包含games
数组。
您希望为代码添加更多防御,以处理外部数据无法满足预期的情况:
if (!empty($data->response->games)) {
foreach ($data->response->games as $game) {
if ($game->appid == $game_id) {
$playtime = $game->playtime_forever;
if (($playtime < 5400) && ($playtime > 1)) {
//echo 'Playtime for ', $usernumber, ' is ' . $playtime."<br>";
echo $usernumber."<br>";
}
}
}
}
} else {
echo 'Games missing!';
}
答案 1 :(得分:0)
$data->response->games
此密钥似乎不存在于您的JSON响应中。尝试在json_decode
电话后var_dump($data->response);
写一下自己调试一下。{{1}}。这应该可以帮助您了解这里出了什么问题。