PHP Curl在分页时使用Limit和Offset进行While循环
我是这里的新手,想了解一下PHP循环 我想从myendpoint获取“itemid”,并将其存储到PHP Curl在分页时使用Limit和Offset进行While循环,php,loops,while-loop,pagination,Php,Loops,While Loop,Pagination,我是这里的新手,想了解一下PHP循环 我想从myendpoint获取“itemid”,并将其存储到$result\u数组中 Myendpoint已分页 第一页,offset=0,第二页,offset=100等 第一页的最大itemid为100,第二页的最大itemid为100,以此类推 最后一页未知,因此我将count($result\u array)%100==0设置为while条件 逻辑是如果第一页包含100个itemid,则执行第二页,如果第二页包含100个itemid,则执行第三页,以此
$result\u数组中
Myendpoint已分页
第一页,offset=0
,第二页,offset=100
等
第一页的最大itemid为100,第二页的最大itemid为100,以此类推
最后一页未知,因此我将count($result\u array)%100==0
设置为while条件
逻辑是如果第一页包含100个itemid,则执行第二页,如果第二页包含100个itemid,则执行第三页,以此类推
我的脚本只有在第一页包含<100个itemid并且没有第二页时才起作用
如何解决这个问题
多谢各位
这是我的剧本:
$limit = 100;
$offset = 0;
$result_array = array();
$url = 'https://myendpoint/?limit=' . $limit . '&offset=' . $offset;
do {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$data = json_decode($response, true);
curl_close($ch);
$itemIds = array();
foreach ($data["items"] as $row) {
$itemIds[] = $row["itemid"];
}
$result_array = array_merge($result_array, $itemIds);
$offset = $offset + $limit;
} while (count($result_array) % 100 == 0);
print_r($result_array);
非常简单的错误:当您在循环外部构建URL时,您反复使用相同的URL。将其移动到循环中,使其包含在该循环中计算的
$offset
您尝试检查了什么,为什么脚本不能用于更多项目?