0
votes

I'm working on a PHP script and I want it to fetch this from steam api: Steam api example

This is my code so far:

<!DOCTYPE html>
<html>
      
<head>
    <title>
        Test
    </title>
</head>
  
<body style="text-align:center;">

    <form method="post">
<input type="submit" value="OK" name="kk"/> 
</form>

<?php
if(isset($_POST['kk'])) {
    $json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
    $obj = json_decode($json);
    echo $obj->result;
}
?>
     
</head>
  
</html>

How can I fetch it?

1
What is the question here? You are using file_get_contents, does it not fetch the json response (like in the image). If not what are the errors - endeavour
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\status.php:20 Stack trace: #0 {main} thrown in C:\xampp\htdocs\status.php on line 20 - wsYikes
var_dump($object) and take a look at the contents. Obviously you replaced key=STEAM API KEY HERE with your actual api key... - Kisaragi
Done, what about now? - wsYikes
It returned something with the api, but its confuse - wsYikes

1 Answers

0
votes

You are trying to echo an stdClass as per your comments. That is why the error.

You can access the data (marked in red in the attached image ) like this

<?php
if(isset($_POST['kk'])) {
    $json = file_get_contents('https://api.steampowered.com/ICSGOServers_730/GetGameServersStatus/v1/?key=STEAM API KEY HERE');
    $obj = json_decode($json);
    echo $obj->result->services->SessionsLogon;
    echo $obj->result->services->SteamCommunity;
    echo $obj->result->services->IEconItems;
}

If you are more comfortable with arrays instead of stdClass you can use $obj = json_decode($json, true) and access the elements with echo $obj['result']['services']['SessionsLogon'] etc.

To get a human readable output of the stdClass or the array you should use pre and print_r instead of just var_dump. For eg. echo '<pre>' . print_r($obj, true) . '</pre>';