【问题标题】:Using Valve/Steam API to get multiple IDs converted使用 Valve/Steam API 转换多个 ID
【发布时间】:2012-07-18 08:29:46
【问题描述】:

所以这是一个很长的解释。

我有一个 Counter-Strike: Source 服务器,带有用于商店的游戏内插件。此存储将其数据保存在 MySQL 数据库中(对于此实例,名为“存储”)。商店在该数据库中跟踪玩家的钱(在表“用户”中的“信用”列上)。它根据“steam_id”存储客户端(每个客户端唯一)

“steam_id”的格式为(示例):STEAM_0:0:123456789 OR STEAM_0:1:12345789。

我的页面显示了数据库中排名前 1000 的用户(按信用排序)。

我的问题:我需要将这些难看的 steam_id 转换为实际名称。

我现在在哪里:

Steam API Documentation

根据 API 文档,我在查询 API 时必须使用“社区 ID”。如果我想获得多个用户,我可以使用逗号分隔 GET 字符串中的社区 ID。

(http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=APIKEY&steamids=76561197960435530,76561197960435531&format=json)

我有一个函数可以将 steam_id 转换为 API 可接受的 ID。

function SteamID2CommunityID($steam_id){
    $parts = explode(':', str_replace('STEAM_', '' ,$id));
    $communityID = bcadd(bcadd('76561197960265728', $parts['1']), bcmul($parts['2'], '2'));
    return $communityID;
}

有了这个,我可以用这个来制作我的逗号分隔的社区 ID 列表:

while ($row = $mysqli->fetch_assoc($request)) {
      $ids .= ',' . SteamID2CommunityID($row['steamid']) . ',';
}

现在最棘手的部分是,所有这些值都返回到一个 JSON 数组中。我需要添加一些东西,所以当我显示我的数据时,我可以将“steam_id”直接转换为“名称”(使用现有数组)。

输出示例(删除了大多数键和值以使其可读)

    Array (
[response] => Array
    (
        [players] => Array
            (
                [0] => Array
                    (
                        [steamid] => 76561198010207577
                        [personaname] => [rGA] Stainbow
                    )

                [1] => Array
                    (
                        [steamid] => 76561197966653036
                        [personaname] => |K}{R|Mastarious(MNBH)
                    )

            )

    )

   )

那么,我将如何直接从“steam_id”变为名称?

感谢任何可以提供代码和/或建议的人!

【问题讨论】:

    标签: php steam-web-api steam


    【解决方案1】:

    这是another Stack Overflow question的变种副本,更实用,本地化程度较低,但我不妨回答一下。

    假设您的输入 steam_id$INPUT 并且您的最终输出数组存储在 $OUTPUT 中,这是可用于将 steam_id 转换为 personaname 的函数式 foreach 方法:

    /**
     * Convert steam_id to personaname
     * @returns STRING The name associated with the given steam_id
     *          BOOL   FALSE if no match was found
     */
    function steamID_to_name($INPUT, $OUTPUT)
    {
        // This gets the relevant part of the API response.
        $array = $OUTPUT['response']['players'];
    
        // Using your function to convert `steam_id` to a community ID
        $community_id = SteamID2CommunityID($INPUT);
    
        // Linear search
        foreach ($array as $array_item)
        {
            // If a match was found...
            if ($community_id == $array_item['steamid'])
                // Return the name
                return $array_item['personaname'];
        }
    
        // If no match was found, return FALSE.
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2015-06-01
      相关资源
      最近更新 更多