jacobfitz / steamauth
此包已被废弃且不再维护。未建议替代包。
在您的PHP项目中实现Steam登录
1.0
2021-05-19 19:30 UTC
Requires
- php: >=7.2
- ext-json: *
- iignatov/lightopenid: *
README
SteamAuth 允许您在PHP项目中实现Steam登录。
简单、快速、安全...
安装
使用composer安装
composer require jacobfitz/steamauth
请求
要登录用户,您必须首先发出请求。
$request = new \SteamAuth\Request('API_KEY', 'RETURN_URL'); /* Option 1: Redirect the user */ $request->redirectToLogin(); /* Option 2: Present the user with a link or login button */ echo '<a href="' . $request->getLoginURL() . '">Login with Steam</a>';
API_KEY - 这是您的Steam开发者API密钥,您可以通过访问 https://steamcommunity.com/dev/apikey 来找到它
RETURN_URL - 用户成功登录后将被返回到您网站上的URI。例如,如果您的网站是example.com,并且您想在example.com/proc_login上处理响应,则只需将 proc_login
传递给请求构造函数 - 不要使用完整URL!
用户将被重定向到Steam登录页面,一旦他们使用Steam账户登录,他们将被返回到指定的返回URL,并带有来自Steam的响应。
注意:如果您选择使用Steam登录按钮,则必须查阅并遵守Steam品牌指南。
响应
当用户使用Steam登录后返回您的网站时,您需要处理响应,下面是一个示例。
$response = new \SteamAuth\Response('API_KEY'); /* Check if the response is valid */ if ($response->isValid()) { /** * At this point the user is now logged in. * Their data will automatically be saved to the session. */ /* Get the steam user */ $user = $response->getUser(); } else { /* Get errors */ $errors = $response->getErrors(); }
用户
一旦请求已发出,并且已处理响应,您将能够访问有关用户的信息。SteamAuth附带一个方便的类,使处理Steam用户变得容易,但如果您更喜欢使用自己的实现,您可以从 $_SESSION['steamAuth']
变量获取所需的所有信息。
检查用户是否已登录
要检查Steam用户是否当前已登录,您可以这样做
if (\SteamAuth\User::isLoggedIn()) { doSomeStuff(); }
获取用户
要获取当前登录的用户,您可以这样做
$user = \SteamAuth\User::getCurrent();
重新加载用户
用户详细信息不会自动更新,这意味着可能需要从Steam获取最新的信息。这可以通过简单地这样做来实现
$user->reload();
获取用户信息
要获取有关用户的信息,您可以使用各种方法。下面是如何使用每个方法的示例
/* Directly access an attribute, see ATTRIBUTE_* constants for options */ $user->getAttribute($user::ATTRIBUTE_USERNAME); // --------------- Steam ID --------------- $user->getSteamID(); // --------------- Username --------------- $user->getUsername(); // --------------- Avatar --------------- $user->getAvatar(); /* Specify size */ $user->getAvatar($user::AVATAR_SIZE_MEDIUM); // --------------- Status --------------- $user->getStatus(); /* Check status */ if ($user->checkStatus($user::STATE_ONLINE)) { doSomeStuff(); } // --------------- Real Name --------------- /* This isn't always visible, check is is first */ if ($user->hasRealName()) { /* Get real name */ $user->getRealName(); } // --------------- Profile URL --------------- $user->getURL(); // --------------- Community Profile --------------- $user->isCommunityProfile(); // --------------- Time --------------- /* Get account creation time */ $user->getTimeCreated(); /* Get last logoff time */ $user->getLastLogOffTime(); // --------------- Visibility --------------- $user->getVisibility(); /* Check visibility */ if ($user->checkVisibility($user::VISIBILITY_PUBLIC)) { doSomeStuff(); } // --------------- Logout --------------- \SteamAuth\User::logout();