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

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

php - How to use Guzzle with pagination

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

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

1 Answer

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...