2016-04-18 78 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' –

相關問題