Search code examples
phpfile-get-contentssteam-web-api

Steam API call failed to open stream


Hello I'm just working on a private Webpage for fun. I make a call to get a json from the steam API so that I can get the avatar of a user. Here's my code.

function getSteamImage() {
    $key = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
    $json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$key&steamids=76561197960435530');
    $json_array = json_decode($json,true);
    $image = $json_array["response"]["players"]["avatar"];
    return $image;
}

<li><img src="<?= getSteamImage() ?>"></li>

But now im getting this error:

file_get_contents(http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXX&steamids=76561197960435530): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

BTW the Steam key I used here isn't the one I'm using in the script!


Solution

  • Found the problem but its not really my fault.

    //Instead of this
    $json_array["response"]["players"]["avatar"];
    
    //It has to be this
    $json_array["response"]["players"][0]["avatar"];
    

    In players there is an array with index 0.