signes/vbulletin-api-php

此包已被放弃,不再维护。未建议替代包。

此包允许您通过简单的方法调用轻松地将您的系统与 vBulletin 5 API 集成。

v1.0.0 2016-09-18 09:37 UTC

This package is not auto-updated.

Last update: 2022-08-02 17:04:51 UTC


README

Latest Stable Version Build Status Circle CI codecov.io License

  1. 这是什么?
  2. 基本用法
  3. 上下文
  4. API 服务

这是什么?

此包允许您通过简单的方法调用轻松地将您的系统与 vBulletin 5 API 集成。

已与 vB 5.2 进行测试

基本用法

    use Signes\vBApi\Api;
    use Signes\vBApi\ApiConfig;
    use Signes\vBApi\Connector\Provider\GuzzleProvider;
    
    $apiConfig = new ApiConfig($apiKey, $uniqueId, $clientName, $clientVersion, $platformName, $platformVersion);
    $apiConnector = new GuzzleProvider('http://example.com/my-forum/');
 
    $api = new Api($apiConfig, $apiConnector);
    
    $response = $api->callRequest('user.fetchByEmail', ['email' => 'test@example.com']);
  1. 首先创建您的 API 配置文件,它应包括如下信息:

    • vBulletin API 密钥 - 您可以在 vBulletin 控制面板的 API 部分找到它。 |
    • 唯一 ID - 唯一 ID 用于标识您的客户端和平台名称,可以是任何唯一的字符串。请注意,如果您更改唯一 ID,您的请求将被识别为新 API 客户端的请求,并将返回新的 API 客户端 ID。 |
    • 客户端名称 - 您的客户端名称。
    • 客户端版本 - 您的客户端版本。
    • 平台名称 - 您的平台名称。
    • 平台版本 - 您的平台版本。
    use Signes\vBApi\ApiConfig;
    $apiConfig = new ApiConfig($apiKey, $uniqueId, $clientName, $clientVersion, $platformName, $platformVersion);
  2. 当配置对象准备好后,下一步是初始化连接提供者。此提供者负责您的应用程序与 vB 论坛之间的通信。

    默认情况下,您可以使用包含的 Guzzle 提供者,但在您这样做之前,您应该在 composer 中要求 guzzlehttp/guzzle 包。Guzzle 提供者需要在构造函数中包含论坛 URL 并以斜杠结尾。

    use Signes\vBApi\Connector\Provider\GuzzleProvider;
    $apiConnector = new GuzzleProvider('http://example.com/my-forum/');

    如果这还不够好,您可以为提供自己的连接类,只需实现 Signes\vBApi\Connector\ConnectorInterface 接口。

  3. 之后,您就准备好设置 API 服务了,该服务需要 Signes\vBApi\ApiConfigSignes\vBApi\Connector\ConnectorInterface 对象。

    use Signes\vBApi\Api;
    $api = new Api($apiConfig, $apiConnector);
  4. 当 API 服务初始化后,您可以调用任何 API 资源。

    $response = $api->callRequest('user.fetchByEmail', ['email' => 'test@example.com']);
    $response = $api->callRequest('site.getSiteStatistics', []);

上下文

上下文允许您将逻辑封装到单个类中。在底层,上下文也使用原始 vB API 请求。
每个上下文都必须扩展 Signes\vBApi\Context\Context 类,并在构造函数中可以要求不同的参数。

示例代码

    use Signes\vBApi\Api;
    use Signes\vBApi\ApiConfig;
    use Signes\vBApi\Connector\Provider\GuzzleProvider;
    use Signes\vBApi\Context\User\FetchCurrentUserInfo;
    use Signes\vBApi\Context\User\Login;
    
    $apiConfig = new ApiConfig($apiKey, $uniqueId, $clientName, $clientVersion, $platformName, $platformVersion);
    $apiConnector = new GuzzleProvider('http://example.com/my-forum/');
    $api = new Api($apiConfig, $apiConnector);
    
    $currentLoggedInUserContext = new FetchCurrentUserInfo();
    $loginUserContext = new Login('adminUsername', 'adminPassword');
    
    $meFirst = $api->callContextRequest($currentLoggedInUserContext);
    $api->callContextRequest($loginUserContext);
    $meSecond = $api->callContextRequest($currentLoggedInUserContext);
    
    echo $meFirst['username'] // "Guest"
    echo $meSecond['username'] // "adminUsername"

上下文应处理有关请求方法类型、API 请求类型的信息,并返回给定请求所需的全部参数。
对于您的自定义上下文,您还可以重写 parseResponse 方法以返回预期格式,而不是原始 vB API 响应。

可用上下文

  • 用户

    • 注册
    • 登录
    • 获取当前登录用户数据
    • 通过电子邮件获取用户数据
  • 更多上下文即将推出...

API 服务

API 服务为您做什么

  • 发送 init 请求以检索访问令牌、密钥、API 版本和客户端 ID 以供将来请求使用,
  • 记住您的身份(当您发送有效的登录请求时,未来的每个请求都将作为此登录用户调用),
  • 为每个请求准备签名并包含 vBulletin 所需的所有参数,

如果您想与多个vBulletin实例集成,可以使用API服务作为注册表。如果实例未被记住,将返回null

use Signes\vBApi\Api;
(new Api($apiConfig, $apiConnector))->rememberInstance('myInstanceName');
(new Api($apiConfigSecond, $apiConnectorSecond))->rememberInstance('myOtherInstance');

$firstInstance = Api::getInstance('myInstanceName');
$secondInstance = Api::getInstance('myOtherInstance');

延迟连接

API服务构造函数的第三个参数是$lazy,默认值为true。此参数决定init请求的时机。

如果将其设置为true,则init请求将在第一次调用callRequest方法时发送。因此,如果您提供了错误的配置,您将在此处了解到它。

如果将其设置为false,则init请求将在创建API服务实例时发送。

记住配置和连接

可以在PHP请求之间保持有关ApiConfig的信息,并且强烈建议在生产环境中这样做。为此,您可以使用任何缓存服务。只需将ApiConfig存储在缓存中,并为其未来的使用存储其实例。

这为您提供了一个额外的优势,当您登录到用户账户时,此信息将在ApiConfig中记住,并且您不需要在一定时间内调用登录请求(取决于您的缓存过期时间)。

致谢

Pawel Grzesiecki - 开发者 (http://signes.pl/) MIT 许可证