php - 遍历 JSON 请求的多个页面(Guzzle、Laravel、PHP)
问题描述
新开发人员在这里,如果这是一个简单的问题或类似的问题,但我无法准确找到我正在寻找的东西,我很抱歉(这完全可能是我也没有正确地问这个问题)基本上我有一个从 api 返回的分页列表,我不确定能够循环浏览这些页面的逻辑是什么。这是我到目前为止的代码,它非常适合第一页哈哈。
public function handle()
{
//This is the artisan command that runs on a timer and gets all the ticket and update fields that are needed and saves them to the database.
$tickets = $this->pullTicketSummary();
collect($tickets['data'])
->each(function ($currTicket) {
$ticketRow = Tickets::query()->firstOrCreate(['ticket_id' => $currTicket['id']]);
$ticketRow->status_id = $currTicket['status']['id'];
$ticketRow->category_id = $currTicket['category']['id'];
$ticketRow->user_id = $currTicket['assigned_to']['id'];
$ticketRow->jira_issue_id = $currTicket['jira_issue_id'];
$ticketRow->save();
collect($currTicket['updates'])->each(function ($update) use ($currTicket){
$updateRow = Update::query()->firstOrCreate(['update_id' => $update['update_id']]);
$updateRow->ticket_id = $currTicket['id'];
$updateRow->assignee_change = $update['assignee_change'];
});
});
Log::info('All tickets and updates were pulled successfully');
}
protected function pullTicketSummary()
{ //Function makes the guzzle request and returns the response from the happyfox api
$client = new Client();
$request = $client->get('https://happyfox.com/api/1.1/json/tickets/?size=50&page=1',
['auth' => ['N/A']);
$response = json_decode($request->getBody()->getContents(), true);
return $response;
}
事实上,如果这是在我错过之前已经回答过的问题,我是新手,请通过链接向我射击,或者如果您知道任何可以帮助我自己获得答案的文档,那将是很棒的!谢谢!
解决方案
更新您的函数以使用页码:
protected function pullTicketSummary($page)
{ //Function makes the guzzle request and returns the response from the happyfox api
$client = new Client();
$request = $client->get('https://happyfox.com/api/1.1/json/tickets/?size=50&page='.$page,
['auth' => ['N/A']);
$response = json_decode($request->getBody()->getContents(), true);
return $response;
}
//new function only to save the data from tickets variable. Necessary to reuse.
public function saveTicketsOrSomething($tickets)
{
collect($tickets['data'])
->each(function ($currTicket) {
$ticketRow = Tickets::query()->firstOrCreate(['ticket_id' => $currTicket['id']]);
$ticketRow->status_id = $currTicket['status']['id'];
$ticketRow->category_id = $currTicket['category']['id'];
$ticketRow->user_id = $currTicket['assigned_to']['id'];
$ticketRow->jira_issue_id = $currTicket['jira_issue_id'];
$ticketRow->save();
collect($currTicket['updates'])->each(function ($update) use ($currTicket){
$updateRow = Update::query()->firstOrCreate(['update_id' => $update['update_id']]);
$updateRow->ticket_id = $currTicket['id'];
$updateRow->assignee_change = $update['assignee_change'];
});
});
}
然后迭代直到完成所有页面:
$tickets = $this->pullTicketSummary(1); //first time
$numPages = $tickets['numPages']; //update here to get the actual value of number of pages
$this->saveTicketsOrSomething($tickets);
for ($i = 2; $i < $numPages; $i++) { //start on 2, cause we already have fetched page 1
$tickets = $this->pullTicketSummary($i); //other pages
$this->saveTicketsOrSomething($tickets);
}
Log::info('All tickets and updates were pulled successfully');
推荐阅读
- r - 查找重复值并具有引用
- google-analytics - DataStudio Google Analytics 连接器显示某些指标为零
- python-3.x - 在 python 中创建对象并与其他对象分离
- sql-server - 为什么“如果不存在”在维护计划中不起作用?
- python - Celery 任务在没有 celerybeat 的情况下成功后调用自身
- sql-server - 使用 SQL Server/ADO 替代 Index + Seek on Access/DAO
- javascript - 根据条件数组递归过滤对象数组
- spring - Tomcat 7 启动失败并出现 srpingboot
- python - Django:无法使用命令上传带有西班牙语字符的数据
- javascript - 未捕获的 SyntaxError: missing ) 在 codeigniter 中的参数列表之后