通过api-PHP进行分页
我正在尝试构建一个php脚本,它将通过API进行分页。api返回25197XML记录。我能够将开始偏移和结束偏移传递给API,API将返回结果的子集 我面临的挑战是for循环没有捕获1000以内的其余记录 例如,当前for循环处理1000-10001001-20002001-3000等块中的记录。我无法获得最终的块-25000到26000。for循环在24000-25000处停止处理。这给我留下了197个未处理的XML结果通过api-PHP进行分页,php,for-loop,Php,For Loop,我正在尝试构建一个php脚本,它将通过API进行分页。api返回25197XML记录。我能够将开始偏移和结束偏移传递给API,API将返回结果的子集 我面临的挑战是for循环没有捕获1000以内的其余记录 例如,当前for循环处理1000-10001001-20002001-3000等块中的记录。我无法获得最终的块-25000到26000。for循环在24000-25000处停止处理。这给我留下了197个未处理的XML结果 <?php //Set Start and Offset Par
<?php
//Set Start and Offset Parameters
$start_offset = 0;
$end_offset = 0;
$items_per_page = 1000;
$number = 0;
$counter = -2;
for ($count=0; $count<=100; $count++) {
$counter++;
//Validate that the counter is not null
if ($number != null){
echo "\n";
echo file_get_contents($static_url . "/sc_vuln_query-compliance.php?start=$start_offset&end=$end_offset&seq=$counter");
}
//Initialize the start and end offset variables
$end_offset = $number+=$items_per_page;
$start_offset = $number-$items_per_page+1;
//We want to start at record 0, reset start_offset back to 0 instead of 1
if($start_offset == 1) {
$start_offset = $number-$items_per_page;
}
// We are at the end of the total records, display the remaining
if ($number>$total_xml_records) {
$counter = $counter+1;
$padding = $end_offset + $items_per_page;
echo "\n";
echo file_get_contents($static_url. "/sc_vuln_query-compliance.php?start=$start_offset&end=$padding&seq=$counter");
break;
}
}
?>
您的代码至少在这一行有错误$padding=$end\u offset+$items\u per\u page;-在这里,您将最后一个循环的末端再提升1000,因此得到?start=25001&end=27000&seq=25 尝试$padding=$total\u xml\u记录;相反,这将使您得到?开始=25001&结束=25197&顺序=25 无论如何,你的代码相当复杂。试试这个:
$total_xml_records = 25197; // index 0 .. 25196
$offset = 0;
$counter = 0;
while ($offset < $total_xml_records) {
echo "\n";
echo $static_url . "/sc_vuln_query-compliance.php?start=".($offset)."&end=".(min($offset+$items_per_page-1, $total_xml_records-1))."&seq=".($counter++);
$offset += $items_per_page;
}
为什么要使用for循环?我看到您从未使用过$count变量,因此我认为使用whiletrue更有意义,因为当$mumber>$total\u xml\u记录时,您正在中断,$number的值是多少?我看不出有什么变化。秒等号在这条线上做什么$结束偏移量=$number+=$items\u每页;请使您的代码更具可读性,以便我们可以帮助您。您还应避免为变量使用无意义的名称。我说的是$numbersc\u vuln\u query-compliance.php的请求看起来像什么?请提供一个例子。