【问题标题】:How to use Guzzle with pagination如何使用 Guzzle 进行分页
【发布时间】:2019-02-03 17:18:54
【问题描述】:

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

$request_url="http://192.168.0.1:8081/APIServer/public/api/products";
        $client = new \GuzzleHttp\Client();
        $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']));

如何使用 guzzle 进行分页?

【问题讨论】:

    标签: php laravel api laravel-5 guzzle


    【解决方案1】:

    简短的回答:你不能

    长答案: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?

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 2011-06-17
      • 2018-03-04
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多