【问题标题】:Display steam profiles details in cycle [PHP]在循环中显示蒸汽配置文件详细信息 [PHP]
【发布时间】: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


【解决方案1】:

在 Steam 文档中查找一次获取多个用户的方法。参数steamids 表明这是可能的。

如果可以 - 将所有结果从 DB 加载到变量,并收集 steam id,对 steam api 进行一次查询,合并结果并在第二个 foreach 中显示给用户。

如果不可能 - 您必须将这些值存储在缓存中(memcached/redis/database/file,NOT _SESSION)

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    相关资源
    最近更新 更多