注意:尝试获取非对象的属性 - json

时间:2016-04-18 00:12:58

标签: php steam-web-api

在使用Steam登录我的网站后,我收到此错误Notice: Trying to get property of non-object in /home/u852405559/public_html/index.php on line 178

第178行是foreach((array)$json_decoded->response->players as $player) {

这是168到195行。请帮帮我

} elseif($openid->mode == 'cancel') {
    echo 'User canceled auth';
} elseif($openid->validate()) {
    $id = $openid->identity;
    $ptn = "/^http:\/\/steamcommunity\.com\/openid\/id\/(7[0-9]{15,25}+)$/";
    preg_match($ptn, $id, $matches);
    $steamid = $matches[1];
    $url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$steamid";
    $json_object = file_get_contents($url);
    $json_decoded = json_decode($json_object);
    foreach((array)$json_decoded->response->players as $player) {
        $url = "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=$_STEAMAPI&steamid=$steamid&format=json";
        $json_object2 = file_get_contents($url);
        $gamesOwned = json_decode($json_object2);
        $ownsCSGO = 0;
        foreach($gamesOwned->response->games as $game) {
            if($game->appid == 730) {
                $ownsCSGO = 1;
                break;
            }
        }
        if($ownsCSGO == 0) {
            echo "<script> $(document).ready(function() { Materialize.toast('This steam account does not have CS:GO'); }); </script>";
        } else {

1 个答案:

答案 0 :(得分:0)

因为你的get文件内容返回NULL,这意味着php.ini的设置是关闭的,你需要打开它看到php file_get_contents returns null when allow_url_fopen is on

而不是

$json_object = get_file_contents($url);

您也可以使用

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json_object= curl_exec($ch);
curl_close($ch);

然后

$json_decoded = json_decode($json_object);

$json_decoded = json_decode($json_object, true);

然后使用RecursiveIteratorClass将多维json数组转换为更易于解析的关联数组

$assocArray= new RecursiveIteratorIterator(
    new RecursiveArrayIterator($json_decoded),
    RecursiveIteratorIterator::SELF_FIRST);

最后:

foreach ($assocArray as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}