PHP将Json file_get_contains解析为foreach(由于提供的句柄未处于正确状态,因此无法执行请求的操作)

I'm trying to parse an xml file and display members by their ID with a link to their profile :

$xml=simplexml_load_file("http://steamcommunity.com/gid/103582791433452366/memberslistxml/?xml=1");
foreach ($xml->members->children() as $data)
{
    echo '<a href="http://steamcommunity.com/profiles/'.$data.'">'.$data.'</a>'."<br>";
}

It works well.

But when i try to display avatar for each members from a json file (one file by member) I got an error on dreamweaver "The requested operation cannot be carried out because the handle supplied is not in the correct state."

Here is the code :

 $xml=simplexml_load_file("http://steamcommunity.com/gid/103582791433452366/memberslistxml/?xml=1");

    foreach ($xml->members->children() as $data)
    {   
    $player=file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=MY_APY_KEY&steamids=$data");
    $data1=json_decode($player);
        echo '<a href="http://steamcommunity.com/profiles/'.$data.'"><img src="'.$data1->response->players[0]->avatar.'">'.$data.'</a>'."<br>";
    }

But for example, if I put $player and $data1 before foreach and i replace $data by a unique steamID (76561197960435530 for example) like this :

$xml=simplexml_load_file("http://steamcommunity.com/gid/103582791433452366/memberslistxml/?xml=1");
$player=file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=MY_API_Key&steamids=76561197960435530");
$data1=json_decode($player);

foreach ($xml->members->children() as $data)
{
    echo '<a href="http://steamcommunity.com/profiles/'.$data.'"><img src="'.$data1->response->players[0]->avatar.'">'.$data.'</a>'."<br>";
}

It will display members by their ID with a link to their profile and will display the avatar of this steamid(76561197960435530) next to each member. What i want to do is to display avatar of each member next to their ID, not avatar of 76561197960435530.

Also i tried to just put this code :

$xml=simplexml_load_file("http://steamcommunity.com/gid/103582791433452366/memberslistxml/?xml=1");

foreach ($xml->members->children() as $data)
{
    $player=file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=my_api_key&steamids=$data");
    $data1=json_decode($player);

    var_dump($data1);
}

It was extremely long, several minutes but worked on dreamweaver, it displays structured information for each json file. But when i load page on firefox it doesn't work probably because it's too long to load.

You didn't have authorization for it, or your API is not properly entered:

 <?php


  $xml=simplexml_load_file("http://steamcommunity.com/gid/103582791433452366/memberslistxml/?xml=1");

     foreach ($xml->members->children() as $data)
     {   
     $player=file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=MY_APY_KEY&steamids=$data");
     $data1=json_decode($player);

    var_dump($data1);

     }

 ?>

RESULT OF data1:

 PHP Warning: 
 file_get_contents(http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?        key=MY_APY_KEY&steamids=76561198066695135):
 failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
 in /home/marin/a.php on line 8

So, you receive returned error 401, that's is means you are not authorized on HTTP server using username and password or not set up authorized API key for STEAM account.

Author says:

But when i load page on firefox it doesn't work probably because it's too long to load.

Open on Firefox "View Source", than upload file (not reload in source view), then refresh. You would see small bit faster. You need to know what are how is structured of XML files.

Note, you can't change a data on received of remote data.

I use code similar to the following

$playersStr = "";
$players = array();

// Build your $playersStr as a comma seperated string here


// Get players' information from Valve
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=$_STEAMAPI&steamids=$playersStr";
$json_object= file_get_contents($url);
$json_decoded = json_decode($json_object);

// Complete our player array with additional details from the API
foreach ($json_decoded->response->players as $player)
{
    $players[$player->steamid]['steamid'] = $player->steamid;
    $players[$player->steamid]['personaname'] = $player->personaname;
    $players[$player->steamid]['profileurl'] = $player->profileurl;
    $players[$player->steamid]['avatar'] = $player->avatar;
    $players[$player->steamid]['avatarmedium'] = $player->avatarmedium;
    $players[$player->steamid]['avatarfull'] = $player->avatarfull;
}

A couple things to note:

  • The $playersStr is a comma separated string and in my case it built from a database query. You can build it how every you need to as long as it ends up as a string of comma separated profile ids.
  • The $_STEAMAPI is your Steam API key.
  • At the end of this, you'll have a $players array that you can use and loop through to access the fields you are looking for. In this case, it even as all three avatar sizes.