如何使用Guzzle 6从端点检索分页数据?

时间:2019-01-25 14:32:04

标签: asynchronous guzzle serviceclient

我们需要能够通过Discourse API(https://docs.discourse.org)检索网上论坛数据的所有页面。我们正在使用Guzzle 6,并且似乎可以使用Guzzle的 ServiceClient 类的一部分的promises系统来异步检索所有页面,但是文档中没有任何内容可以确切地建议完成(https://github.com/guzzle/command/blob/master/README.md)。这是(大约)我们现在正在做的事情。

use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Description;
use Webbj74\JSDL\Loader\ServiceDescriptionLoader;

/**
 * @return GuzzleClient|null
 */
public function getClient()
{
    $config = [
        'base_uri' => 'https://connect.mycompany.org',
        'api_key' => 'XYZ',
        'api_username'  => 'system',
    ];


    $client = new Client($config);

    $this->serviceDescription = $this->loadServiceDescription();

    if (! empty($this->serviceDescription)) {
        $this->client = new GuzzleClient($client, $this->serviceDescription);

        return $this->client;
    }

    return null;
}

/**
 * @return Description|null
 */
protected function loadServiceDescription()
{
    $loader = new ServiceDescriptionLoader();

    try {
        $serviceDescription = new Description($loader->load($path = self::SERVICE_JSON));

        return $serviceDescription;
    } catch (\Exception $e) {
        $errorMessage = $e->getMessage();
        Logger::log('Error: ' . $errorMessage, 'ERROR', true);
    }

    return null;
}

服务描述包括以下内容:

{
    "operations": {
    "Groups": {
        "extends": "discourse.base",
      "httpMethod": "GET",
      "uri": "/groups.json"
    }
}

任何指导将不胜感激。

0 个答案:

没有答案
相关问题