如何使循环函数调用分页? [英] How to make looped function calls for pagination?

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

问题描述

我正在尝试使用 Airtable API 从那里的数据中检索记录-具体来说,是我在列单元格中拥有的URL.

I am attempting to use the Airtable API to retrieve records from my data there - specifically, a list of URLs I have in column cells.

我编写了一个函数get_airtable_records,以通过curl进行API调用,并且它起作用-将结果作为Json对象返回.具体来说,我正在将URL推送到数组$article_urls.

I wrote a function, get_airtable_records, to do the API call via curl and it works - returning results as a Json object. Specifically, I am pushing the URLs to an array, $article_urls.

唯一的问题是,Airtable将结果返回限制为最多100条记录的页数",而我的数据包含的内容不止于此.该API接受参数maxRecordspageSize,但更重要的参数pageSize仍设置为100.

The only problem is, Airtable limits the return of results to "pages" of a maximum 100 records, and my data contains more than that. The API accepts parameters maxRecords and pageSize but the more important one, pageSize, is still capped at 100.

Airtable还会返回的另一个Json值offset,在这种情况下用于分页. offset是旨在用作输入参数(也称为offset)的记录ID. 您可以使用它来表示随后的其他API调用中的开始记录.我明白这一点.

What Airtable does also return is another Json value, offset, which is used in such cases for pagination. The offset is a record ID intended to be used as an input parameter (also called offset). You can use it to denote the starting record in a subsequent additional API call. I understand this.

我不了解如何修改我的代码,以解决可能需要再次轮询Airtable的可能性.

What I don't understand is how to modify my code to account for the possibility of needing to poll Airtable again.

换句话说,当没有offset值时,我们应该始终从头开始运行. 然后,如果在返回的结果中出现offset值,我们应该再试一次-直到不存在offset值.

In other words, we should always do a starting run from scratch, when there is no offset value. Then, if an offset value is present in returned results, we should go around again - until an offset value is not present.

这就是我所拥有的.

  // Make get request, store result in array
  $articles = get_airtable_records($offset); // $offset won't exist at the start

  // Prepare Article URLs list as an array
  if (!isset($article_urls)) {
     $article_urls = array();
  }

  // For each URL found in Airtable
  foreach($articles['records'] as $record){
     $url = $record['fields']['Published URL'];
     // Add to our array list
     if (!empty($url)) {
        array_push($article_urls, $url);
     }
  }

  // URL list after first pass:
  echo '<pre>';
  print_r($article_urls);
  echo '</pre>';
  // May hit a max of 100

  // echo 'Offset: ' . $articles['offset'];
  // Value like "itrJYSLx0RfslI80f/recEu6TiPTPCSDxg5" may exist.
  // If so, go back to start, do get_airtable_records($offset) again and array_push
  // Until, once more there is no "offset" value at end

我推测某种while循环会很有用...?

I am speculating that some sort of while loop will be useful... ?

有些事情是真的...

A couple of things are true...

  • 在第一次调用中,因为它从记录0开始,所以不需要传递任何原始offset值.
  • 但是该遍及后续遍可能会生成一个offset值,该值应用于进行另一遍遍.
  • final 调用将不会生成offset值,因为它将返回用尽结果的最后一页,因此无需重新开始.
  • In the first call, there will be no originating offset value needing to be passed, since it starts from record 0.
  • But that and subsequent passes may generate an offset value, which should be used to make another pass.
  • The final call will not generate an offset value, since it will have returned the final page of exhausted results, and there is no need to start again.

推荐答案

在很大程度上感谢 @anthony在这里对类似问题的回答,我似乎有一些有效的代码...

Thanks largely to @anthony's answer to a similar question here, I seem to have some working code...

  // Prepare Article URLs list as an array
  $article_urls = array();

  // Call Airtable records in pages of 100 max
  do {

        // Offset is either inherited from last page's results, or is nothing
        $offset = $articles['offset'] ?: "";

        // Make get request, store result in array
        $articles = get_airtable_records($offset);

        // For each URL found in Airtable
        foreach($articles['records'] as $record){
           $url = $record['fields']['Published url'];
           // Add to our array list
           if (!empty($url)) {
              array_push($article_urls, $url);
           }
        }

  } while(!empty($articles['offset'])); // If there's an offset value (ie. starting record of next page), do again

  // Output URL list for check
  echo '<pre>';
  print_r($article_urls);
  echo '</pre>';

解释似乎是:

使用do while循环. 首先,将offset设置为从上一次运行继承的值,或者不设置任何值.

Use a do while loop. At the start of this, set offset to be either the value inherited from the previous run, or nothing.

我的get_airtable_records函数已经限制了API调用中是否存在offset,以下内容将offset查询字符串添加到下一个API调用的URL中(如果存在).

My get_airtable_records function was already limiting the presence or not of offset in the API call, with the following, which adds the offset query string to the URL for the next API call if one is present...

  if (!empty($offset)) {
    $q_offset = '&offset='.$offset;
  }

我已经对此进行了测试,它使我从$article_urls数组的两页中获得了全部137个结果.我没有用超过两页的结果进行测试.

I have tested this and it gave me all 137 results from two pages in to my $article_urls array. I haven't tested it with any more than two pages of results.

这篇关于如何使循环函数调用分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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