0

Working with Steam API, I'm stuck with the following problem: when trying to get player achievements, it's perfect - when it's returing results, something like this:

{
   "playerstats":{
      "steamID":"xxxxxxxxxxxxxxxxx",
      "gameName":"Life is Strange™",
      "achievements":[
         {
            "apiname":"AC_1",
            "achieved":1,
            "unlocktime":1620689085
         },
         {
            "apiname":"AC_2",
            "achieved":1,
            "unlocktime":1620677605
         },
         /* ETC, ETC...  */
         {
            "apiname":"AC_60",
            "achieved":0,
            "unlocktime":0
         }
      ],
      "success":true
   }
}

But in some cases, a product can return empty, because it doesn't have achievements, like this:

{
   "playerstats":{
      "error":"Requested app has no stats",
      "success":false
   }
}

And then I get an error Bad Request 400 for the file_get_contents.

My code is:

$json_achv = file_get_contents('https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid='. $the_appid .'&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&steamid='. $steam_userid);
$data_jprog = json_decode($json_achv,true);

I have tried this, same result (Bad Request 400). And using curl, the json_decode returns NULL.

Any thoughts?

Daniel Lemes
  • 279
  • 4
  • 21
  • What exactly are you looking to achieve here? A bad request will be a bad request no matter which function you use to perform it. The questions you're linking to are talking about error handling, but from what I can gather, you're expecting those solutions to yield actual response data? – El_Vanja May 11 '21 at 22:37
  • I'm trying to understand why it's returning bad request even when the json exists and it's returning results (if I access it directly). So I don't see a reason for a bad request. – Daniel Lemes May 11 '21 at 22:38
  • Seems like a question for Steam support, rather than Stack Overflow. Does the 400 status disrupt your application flow in any way? – El_Vanja May 11 '21 at 22:44
  • 1
    I think this is going to be the way `file_get_contents` is designed; if you get a 4xx it's a client error, so you get nothing. This is also the way Javascript `fetch` operates, for example. While you are right @El_Vanja that a bad request is a bad request, it's extremely common for error responses with 4xx to contain content (have you ever seen a 404 page?). Going forward, I think you'll need to treat a `null` response as though there was nothing to return. – msbit May 11 '21 at 23:14
  • I can go ahead removing some function (achievement progress), but to be honest, I have no idea what I'm doing wrong, since there is a json response even when there are no achievements (the "success: error"), so it shouldn't be happening, I suppose. – Daniel Lemes May 11 '21 at 23:28

1 Answers1

0

After a few more tests, I found the 400 Bad Request was related to UTF8 error. Unable to modify headers, I made the file_get_contents ignore errors.

$the_json = 'https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid='. $the_appid.'&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&steamid='. $steam_userid;
$context = stream_context_create(array('http' => array('ignore_errors' => true)));
$json_achv = file_get_contents($the_json, false, $context);
$data_jprog = json_decode($json_achv,true);

Now I can test the "success" true or false.

if ($data_jprog['playerstats']['success'] !== true) {
 echo 'no achievements';
}
else {
 echo 'Total achievements'. count($data_jprog['playerstats']['achievements']);
 // blabla whatever you want to do 
}  

I'm not sure why I tried it before and it failed, but it's working now. Thank you.

Daniel Lemes
  • 279
  • 4
  • 21