【发布时间】:2018-02-08 22:55:48
【问题描述】:
我正在尝试在 php 页面中显示捐赠者列表。捐赠者的所有信息都在数据库中。有一个php代码:
$apikey = 'my_apikey_here';
$host = 'db_host';
$user = 'db_user';
$password = 'db_user_pw';
$database = 'db';
$connection = new mysqli($host, $user, $password, $database);
if (mysqli_connect_error())
{
die('Error connect to Batabase: (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = "SELECT * FROM `donate_top_donors` ORDER BY amount DESC";
$result = $connection->query($query);
while($row = mysqli_fetch_array($result))
{
$id = $row['id'];
$user = $row['user'];
$steamid64 = $row['steamid64'];
$amount = $row['amount'];
$date = $row['unix_date'];
$response = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" . $apikey . "&steamids=" . $steamid64);
$json = json_decode($response, true);
$avatar = $json['response']['players'][0]['avatar'];
$personaname = $json['response']['players'][0]['personaname'];
echo "
<div class='col'>
<div class='donation-container'>
<div class='Column Column_narrow'>";
if(isset($personaname))
{
echo "<a href='https://steamcommunity.com/profiles/".$steamid64."' target='_blank'><img src='".$avatar."' alt='profile avatar'></a>
</div>
<div class='Column Column_fluid'>
<a href='https://steamcommunity.com/profiles/".$steamid64."' target='_blank'>".$personaname."</a>";
}
else
{
echo "<a href='https://steamcommunity.com/profiles/".$steamid64."' target='_blank'><img src='https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb.jpg' alt='profile avatar'></a>
</div>
<div class='Column Column_fluid'>
<a href='https://steamcommunity.com/profiles/".$steamid64."' target='_blank'>".$user."</a>";
}
echo "<p><strong><span style='font-size: 15px;'>.".$amount." €</span></strong></p>
</div>
</div>
</div>";
}
数据库截图:https://image.prntscr.com/image/wVIrQWf-ReSB1YmaDZuPsQ.png
网页截图:https://image.prntscr.com/image/ATa6bbL5T1ydiIMfF3vGcw.png
似乎一切都很好,但一个大问题是我在网页加载时等待了几分钟。我认为发生这种情况的原因是因为
$response = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" . $apikey . "&steamids=" . $steamid64);
循环调用while,但我不知道如何以另一种方式进行。大家有什么建议
谢谢。
【问题讨论】:
-
根据他们的文档,该 API 端点将采用逗号分隔的最多 100 个 ID 的列表。
标签: php steam-web-api