使用PHP遍历分页数据JSON [英] Loop through paginated data JSON using PHP

查看:181
本文介绍了使用PHP遍历分页数据JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用REST API,该API以分页格式返回数据. 1页将有100条记录.如果有更多数据,则将定义一个"hasMore"参数和"offset"参数,您可以使用它们来检索结果的下一页.

I'm working with a REST API that returns data in paginated format. 1 page will have 100 records. If there is more data there will be a "hasMore" parameter and "offset" parameter defined which you can use to retrieve the next page of results.

到目前为止,我有以下代码:

So far I've got the following code:

function getData(){
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "ENDPOINT",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache"
      ),
    ));
    $response = curl_exec($curl);
    $data = json_decode($response);

    //if there is more get them.....
    if($data->hasMore == 1){ //make another request...
        while($data->hasMore == 1){ 
            $offset = $data->offset;
            $curl = curl_init();
            curl_setopt_array($curl, array(
              CURLOPT_URL => "ENDPOINT",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => "",
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 30,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => "GET",
              CURLOPT_HTTPHEADER => array(
                "cache-control: no-cache"
              ),
            ));
            $response = curl_exec($curl);
            $data = json_decode($response);
            $allData = array_merge($data->data, $data->data);
        }
    }
    return $data; //return the results for use
}

我的问题是我正在努力让它检查是否有更多数据,以及是否在同一功能内都请求下一页.我希望这是有道理的,并且希望您能提供任何帮助或建议.

My issue is that I'm struggling to have it check if there is more data and if there is request the next page all within the same function. I hope this makes sense and I'd appreciate any help or advice you can offer.

由于我知道还有另一页,现在我可以使用它.问题是如何创建它,以便它将检查参数以查看是否有更多参数,以及是否存在另一个页面请求并将其追加到现有数据数组中.

Right now I have it working as I know there is another page. The issue is how can I create it so that it will check the parameter to see if there is more and if there is another page request and append to the existing array of data.

推荐答案

基于@TommyLee答案,但使用返回的偏移量(只是猜测应该如何使用它):

Based on @TommyLee answer, but using the returned offset (just guessing how it is supposed to be used):

function getData($offset = 0){
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "ENDPOINT" . "?offset=" . $offset,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache"
      ),
    ));
    $response = curl_exec($curl);
    $data = json_decode($response);

    return $data; //return the results for use
}

$allData = array();

do {
    $offset = $data->offset ?: 0;
    $data = getData($offset);

    foreach($data->data as $row){
         $allData[] = $row;
    }
} while($data->hasMore == 1);

这样,如果没有任何内容传递给getData(),则假定偏移量为0,否则,将上一个响应的下一个偏移量传递给它.

This way, if nothing is passed to getData(), it assumes the offset is 0, otherwise, you pass it the next offset from the last response.

同样,还不清楚如何使用偏移量,因此您可能需要调整我的示例以适合$offset的正确用法.

Again, how the offset is used isn't clear, so you might need to adjust my example to fit the proper usage for $offset.

这篇关于使用PHP遍历分页数据JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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