0

Here is code:

    <?php
$userFriends = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=[api-key]&steamid=[steam-id]&relationship=all"), true);
foreach ($userFriends->data as $friend) {
    //$json = json_decode(file_get_contents("http://steamcommunity.com/market/priceoverview/?appid=730&market_hash_name=".rawurlencode($item)), true);
    echo $friend->friends;
}
?>

Can't parse. Where problem?

James
  • 3,765
  • 4
  • 48
  • 79
  • what do you mean by can't parse ? What is the value of `$userFriends` ? – Unex Apr 27 '16 at 15:58
  • What's the json error (if any)? – m02ph3u5 Apr 27 '16 at 15:59
  • 1
    Because `$userFriends` doesn't have a `data` property, it has `friendsList` Also, you specifically ask `json_decode` to return you an array, not an object. – Jonnix Apr 27 '16 at 16:02
  • u should access it like this. $userFriends['friendsList']. – kanchan Apr 27 '16 at 16:04
  • Ok. Where problem now? friends; } ?> – M. Dmitriev Apr 27 '16 at 16:08
  • 1
    @M.Dmitriev You should remove your actual API keys from your post. – James Apr 27 '16 at 16:08
  • var_dump the variable $userFriends. Is it null? Then probably the json string you are getting back isn't valid. –  Apr 27 '16 at 16:09
  • Your question has been edited by @James to remove the keys, which is good. And I have done the same for the answer which copied your keys from your post. But you really should flag your post with a custom flag for a moderator, to enlist their help in purging the keys from the edit histories completely. Either that, or contact Steam operations and have them revoke the keys and issue you new ones. – Peter Duniho Apr 27 '16 at 16:59

3 Answers3

1

First of all, if you want access to JSON data with oo -> syntax, you have to remove True parameter from json_decode:

$userFriends = json_decode( file_get_contents( ... ), true );

Then, your JSON has this structure:

{
    "friendslist": {
        "friends": [
            {
                "steamid": "0123456789",
                "relationship": "friend",
                "friend_since": 0
            },
            (...)
        ]

    }
}    

So, to access to friends, you have to write something like this:

foreach( $userFriends->friendslist->friends as $friend )
{
    echo $friend->steamid . PHP_EOL;
    echo $friend->relationship . PHP_EOL;
    echo $friend->friend_since . PHP_EOL;
}
fusion3k
  • 11,568
  • 4
  • 25
  • 47
0

You can use this to get the last JSON error in case there was one. Of course, just echo the error instead of using my exact code.

    switch (json_last_error()) {
        case JSON_ERROR_NONE:
            //echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            header($_SERVER["SERVER_PROTOCOL"].'500 Maximum stack depth exceeded');
            $this->response = array('error' => 'JSON Decode error - Maximum stack depth exceeded');
            return $this->return_response();
        break;
        case JSON_ERROR_STATE_MISMATCH:
            header($_SERVER["SERVER_PROTOCOL"].'500 Underflow or the modes mismatch');
            $this->response = array('error' => 'JSON Decode error - Underflow or the modes mismatch');
            return $this->return_response();
        break;
        case JSON_ERROR_CTRL_CHAR:
            header($_SERVER["SERVER_PROTOCOL"].'500 Unexpected control character found');
            $this->response = array('error' => 'JSON Decode error - Unexpected control character found');
            return $this->return_response();
        break;
        case JSON_ERROR_SYNTAX:
            header($_SERVER["SERVER_PROTOCOL"].'500 Syntax error, malformed JSON');
            $this->response = array('error' => 'JSON Decode error - Syntax error, malformed JSON');
            return $this->return_response();
        break;
        case JSON_ERROR_UTF8:
            header($_SERVER["SERVER_PROTOCOL"].'500 Malformed UTF-8 characters, possibly incorrectly encoded');
            $this->response = array('error' => 'JSON Decode error - Malformed UTF-8 characters, possibly incorrectly encoded');
            return $this->return_response();
        break;
        default:
            header($_SERVER["SERVER_PROTOCOL"].'500 Unknown JSON Decode error');
            $this->response = array('error' => 'JSON Decode error - Unknown error');
            return $this->return_response();
        break;
    }
James
  • 3,765
  • 4
  • 48
  • 79
  • No answer. Error: Warning: Invalid argument supplied for foreach() in C:\OpenServer\domains\localhost\index.php on line 75 – M. Dmitriev Apr 27 '16 at 16:03
  • Then there was no error in decoding the JSON. I see a comment on your original post that looks like it's your answer though. If not, please supply the JSON, and also the decoded JSON array. – James Apr 27 '16 at 16:04
0

Can read it as an array

<?php
$userFriends = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=[api-key]&steamid=[steam-id]&relationship=all"), true);

foreach ($userFriends['friendslist']['friends'] as $friend) {
    echo $friend['steamid'];
    echo $friend['relationship'];
    echo $friend['friend_since'];
}
?>
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Jason
  • 171
  • 9