Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何循环使用此API数据_Php_Api_Pagination_Limit_Offset - Fatal编程技术网

Php 如何循环使用此API数据

Php 如何循环使用此API数据,php,api,pagination,limit,offset,Php,Api,Pagination,Limit,Offset,我有一个API,我正在向它请求数据 问题是我不想强调我的应用程序,因为数据集可以包含数以万计的对象 返回数据的方式如下所示,我可以指定页面限制和偏移量,如limit=10和offset=10: "objects": { "object", "object2", //and so on.... } "hasMore": true, "count": 23123, count属性保存对象总数,如果有更多对象要获取,则hasMore等于true;如果没有对象剩余,则hasMo

我有一个API,我正在向它请求数据

问题是我不想强调我的应用程序,因为数据集可以包含数以万计的对象

返回数据的方式如下所示,我可以指定页面限制和偏移量,如limit=10和offset=10:

"objects": {
    "object",
    "object2",
    //and so on....
}
"hasMore": true,
"count": 23123,
count属性保存对象总数,如果有更多对象要获取,则hasMore等于true;如果没有对象剩余,则hasMore等于false

如果hasMore等于true,我的假设是我需要发出一个新请求,偏移量等于我页面大小的两倍,直到hasMore等于false,对吗

我尝试过类似的方法,但没有成功,因为我留下了一个几乎为空的对象,只有hasMore等于false和total count:

 $pageSize = 10;
 $pages = ceil(23123 / $pageSize); // total count of objects
 for ($page = 0; $page <= $pages; $page++) {
    $products = $api->get_products($page * $pageSize, $pageSize);
    var_dump(json_decode($products));
 }
$pageSize=10;
$pages=ceil(23123/$pageSize);//对象总数
对于($page=0;$page get_产品($page*$pageSize,$pageSize);
变量转储(json解码($products));
}
应该可以

$products = json_decode($products, true)

foreach ($products["objects"] as $object) {
    echo $object;
}
应该有用

$products = json_decode($products, true)

foreach ($products["objects"] as $object) {
    echo $object;
}
同意@miken32

假设它是
$api->getproducts($offset,$limit)
,请尝试以下操作:

$pageSize      = 10;
$currentPage   = 0;
$hasmore       = true;

while( $hasmore === true ) {
    $currentOffset = $currentPage * $pageSize;
    $products      = json_decode( $api->get_products( $currentOffset, $pageSize ), true );
    $hasmore       = $products['hasMore'];
    processProducts( $products['objects'] );
    $currentPage++;
}
同意@miken32

假设它是
$api->getproducts($offset,$limit)
,请尝试以下操作:

$pageSize      = 10;
$currentPage   = 0;
$hasmore       = true;

while( $hasmore === true ) {
    $currentOffset = $currentPage * $pageSize;
    $products      = json_decode( $api->get_products( $currentOffset, $pageSize ), true );
    $hasmore       = $products['hasMore'];
    processProducts( $products['objects'] );
    $currentPage++;
}

这似乎是一个
循环的工作,但不清楚问题出在哪里?我关心的一个问题是,这是否是“正确的方法”从API中分割数据。我不想给服务器增加压力,而是想使用某种带限制和偏移量的分页。这似乎是
循环的工作。不清楚问题出在哪里?我关心的一个问题是,这是否是“正确的方法”从API中分割数据。我不想给服务器增加压力,而是想使用某种带限制和偏移量的分页。
$hasmore
不是字符串。您还需要将truthy值传递给
json_decode()
以获取数组。
$hasmore
不是字符串。您还需要向
json\u decode()
传递真实值以获取数组。