compredict / users-sdk
允许PHP应用程序创建/修改用户。
Requires
- php: >=7.0
Requires (Dev)
- codeless/jugglecode: 1.0
- php-coveralls/php-coveralls: 2.1
- phpunit/phpunit: ^4.8.35
- vlucas/phpdotenv: ^2.2
This package is not auto-updated.
Last update: 2024-09-28 06:12:32 UTC
README
连接到COMPREDICT V1 REST API的PHP客户端。
了解更多信息,请访问官方文档网站:https://compredict.de
要求
- PHP 7.0 或更高版本
- 启用cUrl扩展
要使用基本认证连接到API,您需要以下内容
- (可选) 管理员API密钥以注册到用户。因为我们只允许管理员注册。
安装
使用以下Composer命令从Packagist上的COMPREDICT供应商安装API客户端
$ composer require compredict/users-sdk $ composer update
命名空间
以下所有示例都假设已使用以下命名空间声明导入到作用域中的Compredict\API\Users\Client
类
use Compredict\API\Users\Client as Compredict;
配置
要在PHP代码中使用API客户端,请确保您可以通过Composer的vendor/autoload.php
钩子访问Compredict\API
。
将您的凭证提供给静态配置钩子,以准备API客户端连接到COMPREDICT平台上的存储
基本认证
$compredict_client = Compredict::getInstance( 'Admin-token', // optional, only needed if the implementers needs to register new users. );
登录用户(GET)
列出集合中的所有算法
$user = $compredict_client->loginUser($username, $password); # another option use Compredict\API\Users\Resources\User as User; $user = User::login($username, $password);
获取用户信息
$user->first_name; $user->last_name; $user->organization; $user->phone_number; $user->email; $user->apiKey;
用户(GET/PUT)
您可以通过通过API再次请求用户信息来刷新用户信息
$user->refersh();
您还可以通过请求用户来更新用户信息
$user->first_name = "Ousama"; $user->last_name = "Esbel"; $user->phone_number = "00491**********"; $user->update();
如果更新失败,则您所做的更改将被之前的值覆盖!
重置密码(POST)
要重置用户的密码,您应该传递用户的电子邮件。用户将收到一封电子邮件,其中包含重置密码的表单链接。
$compredict_client->resetPassword($email);
注册新用户(POST)
为了注册新用户,您应该拥有管理员角色,其中您应该向客户端实例提供令牌或通过setAdminKey@Client
函数传递它。
$compredict_client = Compredict::getInstance($token); // or $compredict_client = Compredict::getInstance(); $compredict_client->setAdminKey($API_KEY);
然后您可以执行以下操作
$user = $compredict_client->registerUser($username, $email, $password1, $password2, $organization, $first_name=null, $last_name=null, $phone_number=null);
或使用静态函数isUser
$user = User::register($username, $email, $password1, $password2, $organization, $first_name=null, $last_name=null, $phone_number=null)
处理错误和超时
由于各种原因,API核心的HTTP请求可能不会总是成功。
如果发生错误,则每个方法都将返回false,您应该在采取行动之前始终检查此情况。
在某些情况下,您还可能需要检查请求失败的原因。这通常发生在您尝试保存一些未正确验证的数据时。
$algorithms = $compredict_client->getAlgorithms(); if (!$algorithms) { $error = $compredict_client->getLastError(); echo $error->code; echo $error->message; }
在错误上返回false并使用错误对象提供上下文对于编写快速脚本很有用,但不是用于更大规模和长期应用的最稳健解决方案。
错误处理的另一种方法是配置API客户端在发生错误时抛出异常。请注意,如果您这样做,您将需要自行捕获和处理异常。客户端的异常抛出行为通过failOnError方法控制
$compredict_client->failOnError(); try { $orders = $compredict_client->getAlgorithms(); } catch(Compredict\API\Error $error) { echo $error->getCode(); echo $error->getMessage(); }
抛出的异常是Error的子类,表示客户端错误和服务器错误。API文档中包含响应代码的文档包含客户端可能遇到的错误条件列表。
验证SSL证书
默认情况下,客户端将尝试验证 COMPREDICT AI Core 使用的 SSL 证书。在不希望进行验证或使用未签名证书的情况下,您可以通过使用 verifyPeer 开关来关闭此行为,这将禁用后续所有请求的证书检查。
$compredict_client->verifyPeer(false);