如何使用guzzle promise获取所有分页数据

问题描述 投票:0回答:1

我使用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个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.