V
V
Vitaly Gavrilov2017-03-28 11:05:53
PHP
Vitaly Gavrilov, 2017-03-28 11:05:53

Can't display avatar via Steam API, how to build the code correctly?

Good afternoon dear users. I've been struggling with the problem for a week now that I can't display avatars through the Steam API.
How my code should work:
• The code takes from the MySQL database the last 15 SteamIDs that were authorized on the site through Steam.
• Displays avatars of SteamID data in a table on the main page of the site using the Steam API.
How it works in this case:
• Everything works fine, but the avatars are not displayed. If you click on the RMB avatar and look at the source code, then there is no link to the image at all.
There we will see something like this:
<img src="" width="50px" height="50px">
Tell me what to fix in the code so that it starts working adequately? I'm already confused...

<div class="panel panel-default">
  <?php	
    include("db_connect.php");
    $usersData = [];
    $steamIds = [];
        $query = "SELECT steam_id FROM users_profiles ORDER BY `id` DESC limit 15";
        $result = mysql_query($query)or die("MySQL Error");
        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
                $usersData[$row['steam_id']] = $row;
        $steamIds = $row['steam_id'];
            }
        }

    $url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=".$steamauth['apikey']."&steamids=" . join(',', $steamIds);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result=curl_exec($ch);
    curl_close($ch);
    $steamData = json_decode($result, true);
    
    foreach($steamData['response']['players'] as &$player) {
      $usersData[$player['steamid']]['steam_avatar'] = $player['avatarmedium'];
      $usersData[$player['steamid']]['steam_avatar'] = str_replace("https","http",$usersData[$player['steamid']]['steam_avatar']);
      $usersData[$player['steamid']]['steam_name'] = $player['personaname'];
    }
    
    $chunked = array_chunk($usersData, 3);	
  ?>
    
  <table class="table table-striped table-responsive table-bordered">
  <tbody>
    <?php foreach($chunked as &$chunked_row): ?>
      <tr>
        <?php foreach($chunked_row as &$chunked_cell): ?>
          <td>
            <center>
              <img src="<?= htmlspecialchars($chunked_cell['steam_avatar']); ?>" width="50px" height="50px"/>
              <?= htmlspecialchars($chunked_cell['steam_avatar']); ?>
            </center>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
    </tbody>
  </table>
</div>

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2017-03-28
@mhthnz

Once I wrote a library for the Steam API, it might come in handy:
https://github.com/mhthnz/SteamPlayer
There are methods for getting a link to an avatar (3 sizes) and downloading an avatar:

// Steam api key
SteamPlayer::$API_KEY = 'api key'; 

// Массив идентификаторов стима
$steamIDs = [
    'xxxxxxxxxxxxxxxxxxxxx', 
    'xxxxxxxxxxxxxxxxxxxxx', 
    'xxxxxxxxxxxxxxxxxxxxx'
];

// Отдает коллекцию объектов SteamPlayer
$SteamPlayerCollection = SteamPlayer::Create($steamIDs);

// Получаем массив инстансов SteamPlayer[] и выводим ссылку на большой аватара у каждого
foreach($SteamPlayerCollection->get() as $player){
    echo $player->avatar(SteamPlayer::AVATAR_LARGE);
}

U
Urvin, 2017-03-28
@Urvin

There is a feeling that for a week you are struggling not with a problem, but with the outside world, which does not want to do your work for you.
Have you mastered var_dump and print_r in a week ?
How did you struggle if the standard way of debugging is to ask questions:
- Is there a connection to the database;
- What is in $usersData and $steamIds after fetching data;
- What $url is formed;
— What is contained in $result;
- What is contained in $steamData after decoding;
- What is contained in $usersData after data addition;
Have you not passed?
What did you do for a week? Created three identical themes on Toaster?
Do you understand what your code does?
$steamIds[] = $row['steam_id'];

U
ukoHka, 2017-03-28
@ukoHka

Is the code correct? With the correct profiles, my pictures are displayed. If you set obviously wrong ones, $result comes with empty players and img is not formed at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question