cobwebinfo/shrek-api-client

Cobweb信息SHREK API的客户端。

1.2.1 2016-10-13 10:50 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:52:35 UTC


README

Build Status

Codacy Badge

这个库可以用来从SHREK API提供的各种公共端点拉取数据。客户端的设计考虑了易用性,只需提供您的API ID和私钥,然后按照以下说明进行操作。

##兼容性

安装

建议的安装方法是通过 composer

php composer.phar require "cobwebinfo/shrek-api-client:1.2.*"

请求访问API。

请通过 enquiries@cobwebinfo.com 联系Cobwebinfo以获取API密钥。

用法

ShrekServiceProvider类为实例化访问API所需的各个客户端提供了一个整洁的包装器。如果您不希望使用它,也可以手动实例化客户端。

您可以按照以下方式获取实例

$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET
]);

我建议将其作为单例添加到您的服务容器中。例如,如果您使用Laravel,可以这样做

$this->app->singleton(\Cobwebinfo\ShrekApiClient\ShrekServiceProvider::class, function() {
            return new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET
            ]);
        });

传递给提供者的数组用于配置。要查看可用选项,请参阅 Config.yaml文件

一旦您有了提供者实例,您可以按照以下方式访问各个客户端

$keywordClient = $provider->getKeywordClient();

然后您可以按照以下方式访问API数据

  try {
        $response = $keywordClient->paginate(1, 4, []);
    } catch(IdentityProviderException $e) {
        var_dump('Authentication error: ' . $e->getMessage());
    }

    if($response->wasSuccessful()) {
        foreach($response->body['data']['items'] as $key => $keyword) {
            echo "<h3> $key </h3>";

            var_dump($keyword);
        }
    }

缓存

请注意:默认情况下,应用程序使用'NullStore'缓存类。这是一个空对象模式的实现,正如您所猜测的,它不会缓存任何内容。如果您打算使用此方法,您需要实现自己的缓存以避免触及API限制。或者,如果您的应用程序支持APC或memcache,您可以使用内置的类来自动处理缓存。为此,使用以下配置

$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET,
                'cache_driver' => 'memcache' OR 'apc'
]);

如果您希望创建自己的缓存实现,则创建一个新的类,该类使用'Cobwebinfo\ShrekApiClient\Cache\Contracts\Store'接口,并将完全限定的名称传递给提供者,如下所示

$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET,
                'cache_driver' => '\Your\Namespace\ClassName'
]);

HTTP客户端

默认情况下使用Guzzle作为HTTP客户端。如果您不希望使用guzzle,则提供了一个替代实现。要使用它,提供以下配置

$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET,
                'http_client' => 'asika'
]);

与缓存一样,如果您选择,您也可以创建自己的HTTP客户端。只需创建一个新的类来实现'Cobwebinfo\ShrekApiClient\Support\HttpRequester'接口,并将完全限定的名称传递进去,如下所示

请注意:类应返回一个'\Psr\Http\Message\ResponseInterface'实例。

$provider = new \Cobwebinfo\ShrekApiClient\ShrekServiceProvider([
                'client_id' => YOUR ID,
                'client_secret'=> YOUR SECRET,
                'http_client' => '\Your\Namespace\ClassName'
]);

配置

该包提供了一个Yaml读取器,如果您想将客户端ID、客户端密钥或其他配置存储在yaml文件中。它的工作方式如下

Yaml::parse(file_get_contents(__DIR__ . '/config.yaml'));;

上述代码将返回一个关联数组,然后您可以将它传递给ShrekServiceProvider。

待办事项

  • 清除/绕过缓存?
  • Packagist