循环遍历 JSON 请求的多个页面(Guzzle、Laravel、PHP)

Looping through multiple pages of a JSON request (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;

    }

由于我是新手,如果这是我错过的之前已经回答过的问题,请通过 link 向我开枪,或者如果您知道任何可以帮助我找到答案的文档我自己回答那太棒了!谢谢!

更新您的函数以使用页码:

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');