【问题标题】:How to get all paginated data using guzzle promise如何使用 guzzle promise 获取所有分页数据
【发布时间】:2019-03-03 04:36:35
【问题描述】:

我正在使用 guzzle 获取单个页面的帖子,它工作正常。但现在的问题是页面在每页上有分页 20 个帖子。我想得到所有的帖子。如何使用 guzzle 做到这一点?

这是我的代码:

  public function __construct()
    {
        $this->client = new Client([
            'base_uri' => 'https://xxxxxx.com/',
            'Content-Type' => 'application/json',
        ]);

    }
    public function post($post)
    {
        $response = $this->client->request('GET', $post);

        $output = $response->getBody()->getContents();
        $data = $this->getData($output);
        return $data;
    }

【问题讨论】:

    标签: php promise guzzle


    【解决方案1】:

    一般来说没有办法做到这一点。 HTTP 作为协议没有指定任何关于分页的内容。所以取决于您使用的服务器。通常响应包含类似

    {
        "page": 5,
        "total": 631
    }
    

    基于此信息,您可以通过添加 ?page=6(也取决于服务器)为下一页创建一个 URL 并请求它。

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多