如何将GUZLE与分页一起使用 [英] How to use Guzzle with pagination

查看:0
本文介绍了如何将GUZLE与分页一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GUZLE从服务器API URL检索API,我想获取数据并使用分页。 我试试这些:

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

如何在拼音中使用分页?

推荐答案

简短回答:您不能

长篇解答:Laravel的分页是用于雄辩的查询,而不是针对您在那里拥有的任何类型的数据。

我建议在前端进行管理。但您可以这样做:

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

根据此问题的公认答案:How to do Pagination for JSON data in PHP?

这篇关于如何将GUZLE与分页一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆