2
I am trying to display the values of a JSON with PHP more unsuccessfully. Code Used:
JSON
{
"friendslist": {
"friends": [
{
"steamid": "76561197960265731",
"relationship": "friend",
"friend_since": 0
},
{
"steamid": "76561197960265738",
"relationship": "friend",
"friend_since": 0
},
{
"steamid": "76561197960265740",
"relationship": "friend",
"friend_since": 0
},
{
"steamid": "76561197960265747",
"relationship": "friend",
"friend_since": 0
}
]
}
}
PHP
$steamid_player = "76561198112612121";
$apikey = "APIKEY";
$amg = file_get_contents("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=$apikey&steamid=$steamid_player&relationship=friend");
$decode = json_decode($amg, TRUE);
foreach ($decode["friendslist"]["friends"][0] as $valor){
$steamid = $valor["relationship"]->steamid;
echo $steamid;
}
Error returned
Warning: Illegal string offset 'relationship' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10 Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10 Warning: Illegal string offset 'relationship' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10 Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10 Notice: Trying to get property of non-object in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\leo\test.php on line 10
But an error is always returned , and so no value is displayed. Where am I going wrong ?
Which error is generating, on which line?
– Ricardo
I went through it now and ended up solving it. What you have to be sure of first is whether the $Decode is returning you a array since the
foreachexpecting a array.– Marcos Vinicius