2016-04-18 98 views
1

我使用Steam登录到我的网站后出现此错误Notice: Trying to get property of non-object in /home/u852405559/public_html/index.php on line 178注意:试图获取非对象的属性 - json

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

建议json_decode不成功。试试'var_dump($ json_decoded);'并参见 – rjdown

+0

@rjdown。检查var_dump值类型,即时猜测对象类型是数组 – AndreL

+0

var_dump返回NULL –

回答

0

因为你获取的文件内容返回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"; 
    } 
} 
+0

它仍然返回错误'注意:尝试获取第178行/xxxxx/public_html/index.php中的非对象的属性'和'警告:无效参数提供了foreach()在/xxxxxpublic_html/index.php在线178' –

+0

你能回应$ json_decoded并在这里发布? – rojobo

+0

我如何回应它?我该怎么办?这里是完整的代码'http:// pastebin.com/CT2XR92V' –

相关问题