Authing 文档文档
快速开始
概念
使用指南
开发集成 V2 arrow
  • V2 文档
  • V3 文档
应用集成
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
快速开始
概念
使用指南
开发集成 V2 arrow
  • V2 文档
  • V3 文档
应用集成
加入 APN
开发集成
多租户(内测版)
控制台文档
多租户控制台
租户控制台
Saas 应用 Demo
旧版
开发集成
  • JavaScript SDK 索引
  • 单点登录(SSO)
  • 登录组件 (Guard)

  • JavaScript / Node.js

  • Java / Kotlin

  • Python

  • C#

  • PHP

  • Go

  • Ruby
  • Delphi
  • Android

  • iOS

  • Flutter

  • 微信小程序
  • 微信网页授权
  • React Native
  • 框架集成

  • Radius
  • 错误代码
  1. 开发集成
  2. /
  3. PHP

¶ Authing - PHP

更新时间: 2022-11-12 10:53:51
编辑

Authing PHP SDK 由两部分组成:ManagementClient 和 AuthenticationClient。

ManagementClient 中进行的所有操作均以管理员的身份进行,包含管理用户、管理角色、管理权限策略、管理用户池配置等模块。

AuthenticationClient 中的所有操作以当前终端用户的身份进行,包含登录、注册、修改用户资料、退出登录等方法。

你应该将初始化过后的 ManagementClient 实例设置为一个全局变量(只初始化一次),而 AuthenticationClient 应该每次请求初始化一个。

¶ GitHub 下载地址

条目说明
支持版本PHP 7.1 +
仓库地址https://github.com/Authing/authing-php-sdk (opens new window)

¶ 安装

我们推荐使用 composer 进行安装, 它可以与一些模块打包工具很好地配合使用。

需要注意你的 php 相关版本,官方版本相关说明 (opens new window)。 官方 sdk 开发采用 php 8.0,经过 Retor 兼容 低版本 php 来使用

默认包 authing-sdk/php 支持 php 7.1 以及以上,如果你想使用自己当前 php 版本的一些特性,可以安装适合自己版本的 sdk。以下是 authing-sdk/php 相关包,版本都与 authing-sdk/php 对齐,列表如下:

  • authing-sdk/php (默认支持 7.1 及 7.1+)
  • authing-sdk/php-71 (支持 7.1 拥有当前版本特性)
  • authing-sdk/php-72 (支持 7.2 拥有当前版本特性)
  • authing-sdk/php-73 (支持 7.3 拥有当前版本特性)
  • authing-sdk/php-74 (支持 7.4 拥有当前版本特性)
  • authing-sdk/php-80 (支持 8.0 拥有当前版本特性)
# 默认 sdk
$ composer require authing-sdk/php

# php-71
$ composer require authing-sdk/php-71

# php-72
$ composer require authing-sdk/php-72

# php-73
$ composer require authing-sdk/php-73

# php-74
$ composer require authing-sdk/php-74

# php-80
$ composer require authing-sdk/php-80

sdk 的使用上并没有差异,只是每个版本各有自己的特性。详情可以参考 php 版本 相关语言特性说明。

¶ 使用管理模块

初始化 ManagementClient 需要 userPoolId(用户池 ID) 和 secret(用户池密钥):

你可以在此了解如何获取 UserPoolId 和 Secret .

use Authing\Mgmt\ManagementClient;

$management = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
// 获取管理员权限
$management->requestToken();

现在 managementClient 实例就可以使用了。例如可以获取用户池中的用户列表:

use Authing\Mgmt\ManagementClient;

$management = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");
// 获取管理员权限
$management->requestToken();
$users = $management->users()->paginate();

¶ 管理模块 包含以下子模块:

管理用户管理角色管理策略管理资源与权限管理用户自定义字段管理应用

¶ 使用认证模块

AuthenticationClient 以终端用户(End User)的身份进行请求,提供了登录、注册、登出、管理用户资料、获取授权资源等所有管理用户身份的方法;此模块还提供了各种身份协议的 SDK,如 OpenID Connect, OAuth 2.0, SAML 和 CAS。此模块适合用于纯后端交互的服务器环境。

¶ 初始化

AuthenticationClient 需要 appId(应用 ID):

你可以在此了解如何获取 AppId .

use Authing\Auth\AuthenticationClient;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});

接下来可以进行注册登录等操作:

use Authing\Auth\AuthenticationClient;
use Authing\Types\LoginByEmailInput;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});
$user = $authentication->loginByEmail(new LoginByEmailInput("test@example.com", "123456"));

完成登录之后,update_profile 等要求用户登录的方法就可用了:

use Authing\Auth\AuthenticationClient;
use Authing\Types\LoginByEmailInput;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});
$authentication->loginByEmail(new LoginByEmailInput("test@example.com", "123456"));

$user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));

你也可以在初始化后设置 AccessToken 参数, 不需要每次都调用类似 loginByEmail 方法:

use Authing\Auth\AuthenticationClient;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});
$authentication->setToken("ACCESS_TOKEN");

再次执行 UpdateProfile 方法,发现也成功了:

use Authing\Auth\AuthenticationClient;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});
$authentication->setToken("ACCESS_TOKEN");

$user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));

¶ 错误处理

统一使用 try catch 处理:

use Authing\Auth\AuthenticationClient;
use Authing\Types\UpdateUserInput;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});
$authentication->setToken("ACCESS_TOKEN");

try {
    $user = $authentication->updateProfile((new UpdateUserInput())->withNickname("nickname"));
} catch (Exception $e) {
    print_r($e);
}

¶ 私有化部署

私有化部署场景需要指定你私有化的 Authing 服务的 API 端点(不带协议头和 Path)和密码加密公钥,如果你不清楚可以联系 Authing IDaaS 服务管理员。

use Authing\Auth\AuthenticationClient;

$authentication = new AuthenticationClient(function ($opts) {
    $opts->appId = "YOUR_APPID";
});

$authentication->setHost('https://your-app.you-authing-service.com');
$authentication->setPublicKey('YOUR_PUBLIC_KEY');
use Authing\Mgmt\ManagementClient;

$management = new ManagementClient("AUTHING_USERPOOL_ID", "AUTHING_USERPOOL_SECRET");

$management->setHost('https://your-app.you-authing-service.com');
$management->setPublicKey('YOUR_PUBLIC_KEY');

¶ 获取帮助

请访问 Authing 论坛 (opens new window)。

上一篇: 管理多租户 下一篇: 用户认证模块
  • GitHub 下载地址
  • 安装
  • 使用管理模块
  • 使用认证模块
  • 初始化
  • 错误处理
  • 私有化部署
  • 获取帮助

用户身份管理

集成第三方登录
手机号闪验 (opens new window)
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客 (opens new window)
GitHub (opens new window)
社区用户中心 (opens new window)

公司

400 888 2106
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司