Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I use Guzzle to retrieve API from server API url and i want to fetch the data and using pagination. I try these:

$request_url="http://192.168.0.1:8081/APIServer/public/api/products";
        $client = new GuzzleHttpClient();
        $response = $client->request('GET', $request_url, [
            'headers' => ['Accept' => 'application/xml',
                          'Authorization' => 'Bearer ' . $token,
                          'Content-Type' => 'application/json'
                         ],
            'timeout' => 120
        ])->getBody()->getContents();

        $responseXml = simplexml_load_string($response);
        $responseArray = json_decode(json_encode($responseXml), true);

        return view('dashboard')->with(array('data'=>$responseArray['stdClass']));

How can i use pagination with guzzle?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
484 views
Welcome To Ask or Share your Answers For Others

1 Answer

Short answer: you can't

Long answer: Laravel's pagination is for eloquent querys not for any of the kinds of data that you have there.

I would recommend managing this at the front end. But you can do something like this:

//results may vary according to your data:
$page = !($request->page) ? 1 : $request->page];
$no_items = 5;
$offset = ($page - 1) * $no_items;
$total_items = count($responseArray);
$total_pages = ceil($total_items / $no_items);
$final = array_splice($responseArray, $offset, $no_items);

Based on the accepted answer for this question: How to do Pagination for JSON data in PHP?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...