如何将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?
相关文章