首页 > 解决方案 > 如何使用 guzzle promise 获取所有分页数据

问题描述

我正在使用 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;
    }

标签: phppromiseguzzle

解决方案


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

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

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


推荐阅读