0

I'm trying to create a website with Steam's login, but when I try to call a value from JSON, it doesn't work. Everything works in the source code, except for getting the JSON value. I've even tried printing the steam ID, so I know that ID works. The URL works also.

Here's my source code:

<?php
require 'openid.php';
try {
    $openid = new LightOpenID('workinganonymouswebsite.com');
    if (!$openid->mode) {
        $openid->identity = 'http://steamcommunity.com/openid';
        header('Location: ' . $openid->authUrl());
    } elseif ($openid->mode == 'cancel') {
        echo 'User has canceled authentication!';
    } else {
        $steamurl = ($openid->validate() ? $openid->identity . '' : 'error');
        if ($steamurl == 'error') {
            print "There was an error signing in.";
        } else {
            $id          = end(explode('/', $steamurl));
            $jsonurl     = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXX&steamids=" . $id . "&format=json";
            $json        = file_get_contents($jsonurl, 0, null, null);
            $json_output = json_decode($json);
            echo $json_output['players']['personaname'];
        }
    }
} catch (ErrorException $e) {
    echo $e->getMessage();
}
?>

Here's the JSON on the website.

{
"response": {
    "players": [
        {
            "steamid": "76561198049205920",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "baseman101",
            "lastlogoff": 1357603378,
            "profileurl": "http://steamcommunity.com/id/baseman101/",
            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd.jpg",
            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd_medium.jpg",
            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/24/24bb7c0505db7efe1f1a602d09a5ea412e0ab4bd_full.jpg",
            "personastate": 1,
            "primaryclanid": "103582791429521408",
            "timecreated": 1316469294,
            "loccountrycode": "US",
            "locstatecode": "VA",
            "loccityid": 3918
        }
    ]

}
}

I've tried googling everything. I'm sorry if there is something I missed.

hakre
  • 193,403
  • 52
  • 435
  • 836
baseman101
  • 352
  • 1
  • 4
  • 19
  • You should tell which error you get. If you don't get any error, enable error reporting to the highest level and enable error logging. follow the error log. So before looking into google and the whole internet, first checkout how you can get more information *from your computer*. – hakre Jan 08 '13 at 01:57
  • It gives me Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\openid\example.php on line 15, but I know this means it can still run with the code line in. I've even tried printing the exploded/end value (it gave me a good result). What else could be the problem? This was the only error. The error log shows nothing out of the ordinary, by the way. – baseman101 Jan 08 '13 at 02:37
  • You wrote getting the json value would not work: `echo $json_output['players']['personaname'];` - so I was at least expecting some warnings about undefined indexes here. You don't get these but that line of code is executed? – hakre Jan 08 '13 at 02:39
  • Well, I could have enabled something wrong. Anyway, I'm completely new to JSON, never really bothered with it. How can I integrate this correctly? – baseman101 Jan 08 '13 at 02:46
  • By understanding what `json_decode` does, here is an example: http://eval.in/6133 - *Edit:* and here is a step-by-step guide how to access from those elements: http://stackoverflow.com/a/6322173/367456 – hakre Jan 08 '13 at 02:48
  • The var_dump returned NULL. I think I know the reason. I'm trying to use an external JSON document (on another website), that appears directly as I posted it, so maybe external documents aren't supported. – baseman101 Jan 08 '13 at 03:02
  • first check `var_dump($json);` - if you can see json, fine. If it is `FALSE`, the loading failed, yes. Perhaps because you have not HTTP support on your server, however that depends on configuration, PHP itself has the feature to open remote documents, maybe just disable with your hoster. – hakre Jan 08 '13 at 03:09
  • I'll try this in XAMPP tomorrow. I don't think it'll work, though. If it doesn't, I'll share the API key privately so you can test it on your own. Perhaps it has something to do with the website. – baseman101 Jan 08 '13 at 03:25
  • By default, [`json_decode`](http://php.net/manual/en/function.json-decode.php) returns a `stdClass` (unless it's actually an array), so you'll have to call `$json_output->response->players[0]->personaname`; with the optional `true` flag (`json_decode($json,true)`), you can get an associate array, so that you can call `$json_output["response"]["players"][0]["personaname"]`. Use `var_dump($json_output)` to see the structure more clearly. – Passerby Jan 08 '13 at 04:11
  • It returned a bunch of errors. Unfortunately, the code couldn't read the URL. I could I have a solution that will just save me time, though. I used file_get_contents to retrieve the JSON code and store it in a variable. Now, it can retrieve the code. – baseman101 Jan 09 '13 at 01:16

1 Answers1

1

Thanks for all of your help. I basically put the JSON code in a variable, retrieving it from the Steam website. This is the best solution and I'm sticking to it.

<?php
require 'openid.php';
try {
$openid = new LightOpenID('blah.com');
if (!$openid->mode) {
    $openid->identity = 'http://steamcommunity.com/openid';
    header('Location: ' . $openid->authUrl());
} elseif ($openid->mode == 'cancel') {
    echo 'User has canceled authentication!';
} else {
    $steamurl = ($openid->validate() ? $openid->identity . '' : 'error');
    if ($steamurl == 'error') {
        print "There was an error signing in.";
    } else {
        $id          = end(explode('/', $steamurl));
        $context = stream_context_create(array('http' => array('header'=>'Connection: close\r\n')));
        $json_source = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $id . "&format=json",false,$context);
        $json_output = json_decode($json_source,true);
        $json_output->response->players[0]->personaname;
        echo $json_output["response"]["players"][0]["personaname"];
    }
}
} catch (ErrorException $e) {
echo $e->getMessage();
}
?>

Thank you, Passerby and hakre for the help.

In the future, I have to create cookies, and all of the easy stuff. I'm actually starting that right now.

baseman101
  • 352
  • 1
  • 4
  • 19