fusionauth/fusionauth-client

PHP编写的FusionAuth客户端

1.53.0 2024-09-03 21:47 UTC

This package is auto-updated.

Last update: 2024-09-05 22:55:14 UTC


README

简介

如果您要将FusionAuth集成到PHP应用程序中,这个库将加快您的开发速度。请确保查看我们的SDK使用建议页面

有关FusionAuth的更多信息和相关文档,请参阅https://fusionauth.io

安装

使用客户端库的最佳方式是使用Composer安装以下包:fusionauth/fusionauth-client。请在项目根目录中运行以下命令。

composer require fusionauth/fusionauth-client

然后,在您的PHP文件中包含composer自动加载器。

require __DIR__ . '/vendor/autoload.php';

示例

设置

首先,您必须确保有一个运行的FusionAuth实例。如果您还没有,最简单的方法是通过Docker安装,但还有其他方法。默认情况下,它将在localhost:9011上运行。

然后,您必须在管理UI中创建一个API密钥,以便调用API端点。

现在,您可以开始使用这个库了!

错误处理

在每次请求之后,您需要检查是否有任何错误并进行处理。为了避免混乱,我们在下一个示例中省略了错误处理,但您应该做一些类似以下的事情。

// $result is the response of one of the endpoint invocations from the examples below

if (!$result->wasSuccessful()) {
    echo "Error!" . PHP_EOL;
    echo "Got HTTP {$result->status}" . PHP_EOL;
    if (isset($result->errorResponse->fieldErrors)) {
        echo "There are some errors with the payload:" . PHP_EOL;
        var_dump($result->errorResponse->fieldErrors);
    }
    if (isset($result->errorResponse->generalErrors)) {
        echo "There are some general errors:" . PHP_EOL;
        var_dump($result->errorResponse->generalErrors);
    }
}

创建客户端

要向API发送请求,首先您需要使用创建的API密钥和FusionAuth运行的服务器地址创建一个FusionAuthClient实例。

$client = new FusionAuth\FusionAuthClient(
    apiKey: "<paste the API Key you generated here>",
    baseURL: "https://:9011", // or change this to whatever address FusionAuth is running on
);

创建应用程序

要创建一个应用程序,请使用createApplication()方法。

$result = $client->createApplication(
    applicationId: null, // Leave this empty to automatically generate the UUID
    request: [
        'application' => [
            'name' => 'ChangeBank',
        ],
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->application);

检查API文档中的此端点

向现有应用程序添加角色

要向应用程序添加角色,请使用createApplicationRole()

$result = $client->createApplicationRole(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc', // Existing Application Id
    roleId: null, // Leave this empty to automatically generate the UUID
    request: [
        'role' => [
            'name' => 'customer',
            'description' => 'Default role for regular customers',
            'isDefault' => true,
        ],
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->role);

检查API文档中的此端点

检索应用程序详细信息

要获取有关应用程序的详细信息,请使用retrieveApplication()

$result = $client->retrieveApplication(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
var_dump($result->successResponse->application);

检查API文档中的此端点

删除应用程序

要删除应用程序,请使用deleteApplication()

$result = $client->deleteApplication(
    applicationId: 'd564255e-f767-466b-860d-6dcb63afe4cc',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response
// Note that $result->successResponse will be empty

检查API文档中的此端点

锁定用户

阻止用户登录,请使用deactivateUser()

$result = $client->deactivateUser(
    'fa0bc822-793e-45ee-a7f4-04bfb6a28199',
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response

检查API文档中的此端点

注册用户

要在应用程序中注册用户,请使用register()

下面的代码还向用户注册添加了一个customer角色和自定义的appBackgroundColor属性。

$result = $client->register(
    userId: 'fa0bc822-793e-45ee-a7f4-04bfb6a28199',
    request: [
        'registration' => [
            'applicationId' => 'd564255e-f767-466b-860d-6dcb63afe4cc',
            'roles' => [
                'customer',
            ],
            'data' => [
                'appBackgroundColor' => '#096324',
            ],
        ],    
    ],
);

// Handle errors as shown in the beginning of the Examples section

// Otherwise parse the successful response

检查API文档中的此端点

问题和支持

如果您在这个库中找到任何错误,请在此处提交问题。注意,对FusionAuthClient类的修改必须在FusionAuth 客户端构建器存储库上完成,该存储库负责生成该文件。

但如果您有任何问题或支持问题,我们很乐意听取您的意见。

如果您购买了包含支持的付费计划,请在您的账户门户中提交工单。了解更多关于付费计划的信息。

否则,请在社区论坛中发布您的问题

贡献

在GitHub上欢迎提交错误报告和拉取请求,请访问https://github.com/FusionAuth/fusionauth-php-client

注意:如果您想修改FusionAuthClient类,您必须在FusionAuth 客户端构建器存储库上进行修改,该存储库负责生成我们支持的所有客户端库。

许可

此代码在Apache v2.0 许可证的条款下作为开源软件提供。

升级策略

此库会自动跟踪 FusionAuth API,并可能接收包含错误修复、安全补丁、测试、代码示例或文档更改的更新。

这些发布版本还可能更新依赖项、语言引擎和操作系统,因为我们将遵循它所使用的基础技术的弃用和停用策略。

这意味着,当一个依赖项(例如语言、框架或操作系统)被其维护者弃用时,此库也将被我们弃用,并最终更新为使用较新版本。