greenhollowtech/ght-api-client-bundle

3.2.0 2016-10-10 21:04 UTC

This package is auto-updated.

Last update: 2024-08-27 08:34:18 UTC


README

此包提供 GHT API 客户端作为 Symfony 中的服务。

安装

获取 Composer 包

要使用 Composer 安装,运行 composer require greenhollowtech/ght-api-client-bundle

将 GHTApiClientBundle 添加到您的 Symfony 应用程序

// app/AppKernel.php

    public function registerBundles()
    {
        return array(
            // ...
            new GHT\ApiClientBundle\GHTApiClientBundle(),
            // ...
        );
    }

配置

主机配置

您可以配置一个或多个主机。每个至少必须包括一个域名

# app/config/config.yml

ght_api_client:
    hosts:
        my_api_host:
            domain: my.apidomain.com

如果发送 HTTPS 请求,设置端口号为 443

            domain: my.apidomain.com
            port: 443

如果使用任何其他端口号,您也可以通过设置 secure 标志来让客户端也发送 HTTPS 请求。注意,如果没有设置端口号而设置此标志,则会被忽略,因为端口号默认为 80。

            domain: my.apidomain.com
            port: 9443
            secure: true

如果您正在向 SSL 证书为自签名的沙盒服务器发送 HTTPS 请求,可以触发客户端来抑制 SSL 验证

            domain: my.apidomain.com
            port: 443
            sandbox: true

用户实体配置

apiKeyapiSecret 属性添加到您的用户实体中,以及相关的获取器和设置器。

如果您已经建立了属性,并且希望使用现有数据,只需配置 API 验证器使用这些属性而不是默认值

# app/config/config.yml

ght_api_client:
    user_entity:
        api_key_property: myKeyPropertyName
        api_secret_property: mySecretPropertyName

期望有标准的 username 属性。如果您的不同,您也可以设置它。

# app/config/config.yml

ght_api_client:
    user_entity:
        api_name_property: myUserName

默认 cURL 选项配置

添加任何应针对每个请求设置的 cURL 选项。有一些选项是内部设置的,因此客户端会忽略它们(在 客户端文档 中列出)。

# app/config/config.yml

ght_api_client:
    curl_options:
        connecttimeout: 20
        timeout: 30

用法

API 客户端服务的最简单用法是在参数中包含主机和用户来获取或发送数据

$apiClientService = $this->get('ght_api_client');

$data = array('some' => 'data to send');

$result = $apiClientService->get('api/path', $data, 'my_api_host', $this->getUser());

这等同于使用 setHostsetUser 方法

$result = $apiClientService->setHost('my_api_host')
    ->setUser($this->getUser())
    ->get('api/path', $data)
;

一旦设置,主机和用户将用于后续请求

$response = $apiClientService->get('api/status', array(), 'my_api_host', $this->getUser());

// JSON response is decoded into an array
if (!empty($response['status']) && $response['status'] === 'ready') {
    $response = $apiClientService->post('api/some/action', array('some' => 'data to post'));
}

您还可以动态配置新主机,在目标主机动态的情况下,然后可以通过引用域名来使用它,就像在 getpost 方法调用中引用任何配置的主机名一样

$apiClientService->setHost('42.10.20.30', 443);

// Check some other API
$response = $apiClientService->get('api/status', array(), 'my_api_host', $this->getUser());

if (empty($response['status']) || $response['status'] !== 'ready') {
    // Make a call to the dynamically set host, same user / API credentials, in this case
    $response = $apiClientService->post('dynamic/api/alert', array(), '42.10.20.30');
}

cURL 选项可以覆盖

$apiClientService->setCurlOptions(array('connecttimeout' => 10));
...
// Reset to the default configurations for subsequent requests
$apiClientService->setCurlOptions();

使用的 GHTApiClient 服务也可以扩展和设置。有关更多信息,请参阅类中的方法文档。