Created
May 15, 2017 05:55
-
-
Save grzegorz-rozycki/eed21e72d3ee1334978a238e7bb0c60c to your computer and use it in GitHub Desktop.
Example of using Steam OpenID service in PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Using Steam Id provider example. | |
* | |
* Requires: iignatov/lightopenid @{link https://github.com/iignatov/LightOpenID} | |
*/ | |
ini_set('log_errors', 0); | |
ini_set('display_errors', 1); | |
ini_set('error_reporting', -1); | |
define('DOMAIN', getenv('DOMAIN')); // FYI doesn't have to be the same as referer, can contain port number | |
define('STEAM_API_KEY', getenv('STEAM_API_KEY')); | |
define('STEAM_PROVIDER_URL', 'http://steamcommunity.com/openid'); | |
define('STEAM_API_USER', 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s&format=json'); // @see https://developer.valvesoftware.com/wiki/Steam_Web_API#GetPlayerSummaries_.28v0002.29 | |
ob_start(); | |
session_start(); | |
require_once __DIR__ . '/vendor/autoload.php'; | |
?> | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>SteamLogin</title> | |
</head> | |
<body> | |
<form action="?login" method="post"> | |
<input type="image" width="114" height="43" border="0" | |
src="//steamcommunity-a.akamaihd.net/public/images/signinthroughsteam/sits_large_noborder.png" /> | |
</form> | |
<?php | |
try { | |
$openId = new LightOpenID(DOMAIN); | |
$isLoggedIn = false; | |
if (!$openId->mode) { | |
if (isset($_GET['login'])) { | |
$openId->identity = STEAM_PROVIDER_URL; | |
header('Location: ' . $openId->authUrl()); | |
} | |
} elseif ($openId->mode === 'cancel') { | |
echo 'User has canceled authentication!'; | |
} else { | |
$isLoggedIn = $openId->validate(); | |
echo 'User ' . ($isLoggedIn ? $openId->identity . ' has ' : 'has not ') . 'logged in.'; | |
if ($isLoggedIn) { | |
$responseText = file_get_contents(sprintf(STEAM_API_USER, STEAM_API_KEY, $openId->identity)); | |
echo 'Plain ID: ', str_replace(STEAM_PROVIDER_URL . '/id/', '', $openId->identity), '<br>'; | |
echo '<pre>' . $responseText . '</pre>'; | |
} | |
} | |
} catch (Exception $e) { | |
echo '<p>' . $e->getMessage() . '</p>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment