遍历JSON请求的多个页面(Guzzle,Laravel,PHP)

问题描述 投票:0回答:1

这里是新开发人员,所以很抱歉,如果这是一个简单的问题或类似的问题,但仍无法准确找到我要寻找的东西(完全可能是我也没有正确询问问题)基本上我有一个从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;

    }

鉴于我是陌生人,如果这是我之前错过的答案,请直接在链接上射击我,或者您知道有任何文档可以帮助我自己获得答案,太棒了!谢谢!

php laravel guzzle artisan
1个回答
0
投票

更新您的功能以使用页码:

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++) {
        $tickets = $this->pullTicketSummary($i); //other pages
        $this->saveTicketsOrSomething($tickets);
    }
    Log::info('All tickets and updates were pulled successfully');
© www.soinside.com 2019 - 2024. All rights reserved.