PHP code example of xpaw / steamid

1. Go to this page and download the library: Download xpaw/steamid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xpaw / steamid example snippets


use xPaw\Steam\SteamID;

try
{
	// Constructor also accepts Steam3 and Steam2 representations
	$s = new SteamID( '76561197984981409' );
	
	// Renders SteamID in it's Steam3 representation (e.g. [U:1:24715681])
	echo $s->RenderSteam3() . PHP_EOL;
	
	// Renders SteamID in it's Steam2 representation (e.g. STEAM_0:1:12357840)
	echo $s->RenderSteam2() . PHP_EOL;
	
	// Converts this SteamID into it's 64bit integer form (e.g. 76561197984981409)
	echo $s->ConvertToUInt64() . PHP_EOL;
}
catch( InvalidArgumentException $e )
{
	echo 'Given SteamID could not be parsed: ' . $e->getMessage();
}

use xPaw\Steam\SteamID;

$accountId = 24715681;

// Create a standard individual SteamID from account ID
$steamId = SteamID::FromAccountID( $accountId );
echo $steamId->RenderSteam3(); // [U:1:24715681]

// Quick conversion to 64-bit format
echo SteamID::AccountIDToUInt64( $accountId ); // 76561197984981409

// Quick conversion to Steam3 format
echo SteamID::RenderAccountID( $accountId ); // [U:1:24715681]

use xPaw\Steam\SteamID;

// Supports various URL formats and direct SteamIDs
$inputs = [
	'https://steamcommunity.com/id/username/',
	'https://steamcommunity.com/profiles/76561197984981409',
	'https://s.team/p/abc-def',
	'76561197984981409',
	'[U:1:24715681]'
];

foreach( $inputs as $input )
{
	try
	{
		$steamId = SteamID::SetFromURL( $input, 'yourVanityResolverCallback' );
		echo $steamId->ConvertToUInt64() . PHP_EOL;
	}
	catch( InvalidArgumentException $e )
	{
		echo "Could not parse: $input" . PHP_EOL;
	}
}

use xPaw\Steam\SteamID;

try
{
	$s = new SteamID( $ID );
	
	if( $s->GetAccountType() !== SteamID::TypeIndividual )
	{
		throw new InvalidArgumentException( 'We only support individual SteamIDs.' );
	}
	else if( !$s->IsValid() )
	{
		throw new InvalidArgumentException( 'Invalid SteamID.' );
	}
	
	$s->SetAccountInstance( SteamID::DesktopInstance );
	$s->SetAccountUniverse( SteamID::UniversePublic );

	var_dump( $s->RenderSteam3() ); // [U:1:24715681]
	var_dump( $s->ConvertToUInt64() ); // 76561197984981409
}
catch( InvalidArgumentException $e )
{
	echo $e->getMessage();
}

use xPaw\Steam\SteamID;

$steamId = new SteamID( '[U:1:123456]' );
$friendCode = $steamId->RenderCsgoFriendCode(); // e.g., "SUCVS-FADA"

// Parse friend code back to SteamID
$steamId2 = ( new SteamID() )->SetFromCsgoFriendCode( $friendCode );
echo $steamId2->RenderSteam3(); // [U:1:123456]

use xPaw\Steam\SteamID;

$steamId = new SteamID( '[U:1:123456]' );
$inviteCode = $steamId->RenderSteamInvite(); // e.g., "abc-def"

echo "https://s.team/p/" . $inviteCode;
echo "https://steamcommunity.com/user/" . $inviteCode;