View Single Post
8088
Veteran Member
Join Date: Jan 2008
Old 02-05-2008 , 10:32   Re: Convert STEAMID to Steam Community ID
Reply With Quote #46

Quote:
Originally Posted by voogru View Post
Ooh that looks spiffy.

Care to share it? I'd like to stick it on my forum :D
Sure, I'd be glad to share it :)

First you need to get the SteamID, you can do this by letting your users fill this in in their user profiles. Then you calculate the connected friendid for it with some php :
PHP Code:
$friendid steam2friend($profilefield['value']);

function 
steam2friend($steam_id){
    
$steam_id=strtolower($steam_id);
    if (
substr($steam_id,0,7)=='steam_0') {
        
$tmp=explode(':',$steam_id);
        if ((
count($tmp)==3) && is_numeric($tmp[1]) && is_numeric($tmp[2])){
            return 
bcadd((($tmp[2]*2)+$tmp[1]),'76561197960265728');
        }else return 
false;
        }else{
            return 
false;
        }
    } 
The result allows you to create links to various Steamcommunity links, for example :
PHP Code:
<a href="http://steamcommunity.com/profiles/'.$friendid.'">View SteamID Page</a
And with the Steam browser protocol you can create more handy links :
PHP Code:
<a href="steam://friends/add/'.$friendid.'">Add to your Steam friends</a
To retrieve the actual status of the user, you need to read out the actual SteamID page at the Steam community and look for unique html tags or parts. And to not 'hammer' the Steam pages every time a user profile is viewed, it is wise to cache the results, for which memcached is a great tool.
PHP Code:
$ret=get_steam_stats($friendid);

function 
get_steam_stats($id){
    
    
$mc=new Memcache;
    if ([email protected]
$mc->connect('localhost', <the port number to which memcached is listening to>)) {
        echo 
'memcached unavailable';
     return 
false;
    }
    
    
$ret[email protected]$mc->get('xd'.$id);
    if (!
$ret){
        
$data=file_get_contents("http://steamcommunity.com/profiles/".$id);    
        
$ret=array(); 
This part reads out the SteamID page for the calculated $friendid. To put usefull information like the online status into an array, you can do the following :
PHP Code:
        $ret['uid']=intval($_GET['u']);
        if (
strpos($data,'<h2>Private Profile</h2>')!==false) {
            
$ret['private']=1;
        }else{
            if (
strpos($data,'status_online.gif')!==false){
                
$ret['online']=1;
            }else if ((
$p1=strpos($data,'<p id="statusInGameText">'))!==false){
                
                
$p2=strpos($data,'<',$p1+25);
                
$tmp=substr($data,$p1+25,$p2-$p1-25);
                
$ret['ingame']=$tmp;
                
$ret['join']='steam://friends/joingame/'.$id;            
            } 
Now that you have the current status of the user, you can show it in the userprofile by doing the following :
PHP Code:
    if (isset($ret['private'])){
        echo 
'Online status unavailable';
    }
    else{
        if (isset(
$ret['ingame'])) 
        echo 
'User is In-Game playing '.$ret['ingame'].' - <a href="'.$ret['join'].'">Join</a>;
        if (isset($ret['
online'])) 
        echo '
User is online';
        if (isset($ret['
offline']))
        echo '
User is offline '.$ret['offline'];

On my vBulletin forums I give users the choice to hide the the Steam Community links. In case you're using vB as well and need help on implementing this, let me know. The code above is quite generic and can be used on any page or forumsoftware.

Quote:
Originally Posted by Scuzzy View Post
We have something similar, but are the icons actually from the steam friends? If so, how'd you translate the steam_friends id into an avatar gif?
The Steam avatars are first downloaded to a local folder on the webserver.

This code takes care of that :
PHP Code:
            if (($p1=strpos($data,'<div class="avatarFull">'))!==false){
                
$p1=strpos($data,'<',$p1+24);
                
$p2=strpos($data,'>',$p1+1);
                
$tmp=substr($data,$p1,$p2-$p1+1);
                
$link_full=get_link_src($tmp);
                
download($link_full,'/folder/where/avatars/should/be/stored/prefix_full'.$ret['uid'].'.jpg');
                
$ret['img_full']=$tmp;
                
                
$tmp=str_replace('_full','',$tmp);
                
$link_small=get_link_src($tmp);
                
download($link_small,'/folder/where/avatars/should/be/stored/prefix_small'.$ret['uid'].'.jpg');
                
$ret['img_small']=$tmp;
            }

function 
get_link_src($data){
    
$p1=strpos($data,chr(34));
    
$p2=strpos($data,chr(34),$p1+1);
    return 
substr($data,$p1+1,$p2-$p1-1);
}
 
function 
download ($url,$target){    
    
$data=file_get_contents($url);
    if (
$data) {
        
file_put_contents($target,$data);    
    }

And then they are shown in the userprofile with this code :
PHP Code:
<img src="/folder/where/avatars/should/be/stored/prefix_small'.$userinfo[userid].'.jpg" alt="Steam avatar"/> 

The result as I have it now can be seen here.
Let me know if things are still unclear :)

Last edited by 8088; 02-05-2008 at 10:36.
8088 is offline