php - 如何使用 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;
}
解决方案
一般没有办法做到这一点。HTTP 作为协议没有指定任何关于分页的内容。所以取决于您使用的服务器。通常响应包含类似
{
"page": 5,
"total": 631
}
?page=6
根据此信息,您可以通过添加(也取决于服务器)为下一页创建 URL并请求它。
推荐阅读
- node.js - 当标准输出超过缓冲区大小时,为什么只发出一个“数据”事件?
- c++ - 虚函数中的“std::out_of_range”
- python - Django - 将会话变量从一个视图传递到另一个视图(“请求”未定义)
- python - 有没有办法使用 Odoo 邮件列表发送消息?
- python - 无论如何在 Azure 函数启动上运行和部署 ubuntu 包?
- python - 我无法在 tkinter 中更新标签
- r - R One-Class SVM - 获得概率输出
- python - 使用 Python 进行 ARIMA 预测
- r - 如何使用 plotmath 更新 ggplot 图例标签
- jquery - jquery根据值更改数据放置