我使用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;
}
一般来说,没有办法做到这一点。作为协议的HTTP没有指定关于分页的任何内容。所以取决于您使用的服务器。通常响应包含类似的内容
{
"page": 5,
"total": 631
}
根据此信息,您可以通过添加?page=6
(也取决于服务器)为其创建下一页的URL并请求它。