Is it possible for me to find the URL to a players avatar in gmod lua? I would like to use it for some MySQL integrated things but I can't seem to find any way to do this.
You can use the [URL="https://developer.valvesoftware.com/wiki/Steam_Web_API"]Steam Web API[/URL].
You'll need an [URL="http://steamcommunity.com/dev/apikey"]API key[/URL] (absolutely free, no hassle).
Assuming you have the user's [URL="http://wiki.garrysmod.com/page/Player/SteamID64"]64-bit SteamID[/URL] (or have the [URL="http://wiki.garrysmod.com/page/Player/SteamID"]32-bit SteamID[/URL] to [URL="http://wiki.garrysmod.com/page/util/SteamIDTo64"]convert[/URL] from), you can use the [URL="https://developer.valvesoftware.com/wiki/Steam_Web_API#GetPlayerSummaries_.28v0002.29"]GetPlayerSummaries[/URL] interface like so:
http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXX&steamids=76561197960435530
You need to replace the XXXXX portion with your API key.
Here's a working example where I use HTTP() to send a request to the API and attempt to get the full avatar URL from it:
[code]local key = "XXXXXXXXXXXXXXXXXX" -- replace with your own from the link above
local struct = {
method = "get",
failed = function( err ) MsgC( Color(255,0,0), "HTTP error: " .. err ) end,
url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/",
parameters = { key = key, streamids = "" }
}
function GetPlayerAvatarURL( sid64, callback )
struct.success = callback
struct.parameters.steamids = sid64
HTTP( struct )
end
local function AvatarFromJSON( json )
local tbl = util.JSONToTable( json )
-- just in case something went wrong with the request
if ( !istable( tbl ) or !tbl.response ) then return false end
-- most likely an invalid player was given, or they haven't set up their community profile(?)
if ( !tbl.response.players or !tbl.response.players[1] ) then return false end
return tbl.response.players[1].avatarfull
end
-- Test example using Garry's 64-bit SteamID
local function callback( code, body, headers )
print( AvatarFromJSON( body ) )
end
GetPlayerAvatarURL( "76561197960279927", callback )[/code]
Hope this helps!
Sorry, you need to Log In to post a reply to this thread.