我是 php 新手,仍在尝试掌握这些概念。
我的问题是,如何根据仅在启动循环后可用的信息来更改是否循环?
这是我想出的代码,带有注释以尝试解释我的想法。
我基本上是在尝试为每个页面完成“业务”,如果有 1 个或多个。
注意:$object 包含可能跨越页面的结果集。如果它确实跨越页面,那么 $object->pagination 将存在,否则它将不存在。$object->pagination->pages = 总页数,$object->pagination->pages = 当前页面。
//get first page. some how this needs to be a loop i'm guessing.
$page = 1;
$url = "api/products/page/" . $page;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,array ("Content-Type: Content-Type: application/json; charset=utf-8"));
curl_setopt($ch, CURLOPT_HTTPHEADER,array ("Accept: application/json"));
curl_setopt($ch, CURLOPT_USERPWD, "username-password");
$contents = curl_exec ($ch);
curl_close ($ch);
$object = json_decode($contents);
//$data returned with product info and pagination info if there is more than one page.
//now check in that first page to see if there is other pages set
if(isset($object->pagination)){
while($object->pagination->page < $object->pagination->pages) {
$page = $page+1 ;
//do some business
} else {//stop going through the pages}
}