2012-07-19 190 views
1

所以這是一個非常長的解釋。使用Valve/Steam API獲取多個ID轉換

我有一個反恐精英:源服務器,有一個商店的遊戲插件。該商店將其數據保存在MySQL數據庫中(對於此實例,名稱爲「商店」)。商店在該數據庫中記錄玩家的錢(在'用戶'表中'列'信用')。它存儲基於'steam_id'的客戶端(對每個客戶端唯一)

'steam_id'的格式是(示例):STEAM_0:0:123456789或STEAM_0:1:12345789。

我的頁面顯示數據庫中的前1000名用戶(按信用排序)。

我的問題:我需要將這些醜陋的steam_id轉換爲實際名稱。

我在哪裏現在:

Steam API Documentation

根據API文檔,我不得不使用 '社區IDS' 當我查詢API。如果我想要獲取多個用戶,可以使用逗號分隔GET字符串中的社區ID。

(http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=APIKEY & steamids = 76561197960435530,76561197960435531 &格式= 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」的名字?

謝謝任何​​能提供代碼和/或建議的人!

回答

1

這是一個another Stack Overflow question的變體副本,它更實用,本地化程度較低,但我不妨回答這個問題。

假設你輸入steam_id$INPUT和你的最終輸出數組存儲在$OUTPUT,這是功能foreach的方法,你可以用它來轉換steam_idpersonaname

/** 
* 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; 
}