Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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 While循环从未完成Amazon产品API/APAIO?_Php_While Loop_Timeout - Fatal编程技术网

PHP While循环从未完成Amazon产品API/APAIO?

PHP While循环从未完成Amazon产品API/APAIO?,php,while-loop,timeout,Php,While Loop,Timeout,我有以下代码: $counter = 0; while($currentPage <= $pages) { sleep(0.1); flush(); $browseNodeLookup->setPage($currentPage); try { $xml = $apaiIO->runOperation($browseNodeLookup); } catch(Exception $e1) { if($cou

我有以下代码:

$counter = 0;

while($currentPage <= $pages) {
    sleep(0.1);
    flush();
    $browseNodeLookup->setPage($currentPage);
    try {
        $xml = $apaiIO->runOperation($browseNodeLookup);
    } catch(Exception $e1) {
        if($counter == 20) {
            break;
        }
    }
    if($xml) {
        $all_elements =  iq_parse_data($xml, $itemcount, $all_elements);
        $currentPage++;
    } else {
        if($counter == 20) {
            break;
        }
    }
    $counter++;
}

下次我应该提供完整的代码,也许有人会发现这一点。谢谢大家。

最有可能的解释是,您的代码实际上并没有达到中断,因为每次迭代计数器都会递增,但当它不是实际页面时,您只检查了正好20 应更改以下代码以更改此问题:

  if($counter >= 20) {
    break;
  }

最有可能的解释是,您的代码实际上并没有达到中断,因为计数器在每次迭代中都会递增,但当它不是实际页面时,您只检查了20个 应更改以下代码以更改此问题:

  if($counter >= 20) {
    break;
  }

从外观上看,只有当($counter==20&&!$xml)时,您才会中断。如果该条件不成立,则永远不会推进$currentPage,因此循环将永远被卡住

为清晰起见,请编辑: 在$xml为falsy时,$counter很可能已经超过了20,这意味着$currentPage没有前进(因此您的while条件仍然为true),但由于$counter大于20,所以您的中断不会被击中。将
$counter==20
更改为
$counter>=20

此外,如果您试图继续前进,直到达到20或$totalPages(以较低者为准),那么您的代码可以简化为

$pages = min(20, $pages);

while ($currentPage <= $pages) {
    sleep(0.1);
    flush();

    $browseNodeLookup->setPage($currentPage);

    try {
        $xml = $apaiIO->runOperation($browseNodeLookup);
        $all_elements = iq_parse_data($xml, $itemcount, $all_elements);
    } catch (Exception $e1) {
        // Any error handling you want to do
    }

    $currentPage++;
}
$pages=min(20,$pages);
while($currentPage setPage($currentPage);
试一试{
$xml=$apaiIO->runOperation($browseNodeLookup);
$all_elements=iq_parse_数据($xml、$itemcount、$all_elements);
}捕获(例外$e1){
//您希望执行的任何错误处理
}
$currentPage++;
}

从外观上看,只有当($counter==20&&!$xml)时,您才会中断。如果该条件不成立,您将永远无法推进$currentPage,因此循环将永远被卡住

为清晰起见,请编辑: 在$xml出错的时候,$counter很可能已经超过了20,这意味着$currentPage没有前进(因此while条件仍然为true),但是由于$counter大于20,您的中断不会被击中。将
$counter==20
更改为
$counter>=20

此外,如果您试图继续前进,直到达到20或$totalPages(以较低者为准),那么您的代码可以简化为

$pages = min(20, $pages);

while ($currentPage <= $pages) {
    sleep(0.1);
    flush();

    $browseNodeLookup->setPage($currentPage);

    try {
        $xml = $apaiIO->runOperation($browseNodeLookup);
        $all_elements = iq_parse_data($xml, $itemcount, $all_elements);
    } catch (Exception $e1) {
        // Any error handling you want to do
    }

    $currentPage++;
}
$pages=min(20,$pages);
while($currentPage setPage($currentPage);
试一试{
$xml=$apaiIO->runOperation($browseNodeLookup);
$all_elements=iq_parse_数据($xml、$itemcount、$all_elements);
}捕获(例外$e1){
//您希望执行的任何错误处理
}
$currentPage++;
}

目前,您的代码看起来有点复杂。您可以通过以下方式简化代码:

while($currentPage <= $pages) {
    sleep(0.1);
    flush();
    $browseNodeLookup->setPage($currentPage);

    try {
        $xml = $apaiIO->runOperation($browseNodeLookup);
        $all_elements =  iq_parse_data($xml, $itemcount, $all_elements);
    } finally {
        $currentPage++;
    }
}
while($currentPage setPage($currentPage);
试一试{
$xml=$apaiIO->runOperation($browseNodeLookup);
$all_elements=iq_parse_数据($xml、$itemcount、$all_elements);
}最后{
$currentPage++;
}
}

目前,您的代码看起来有点复杂。您可以通过以下方式简化代码:

while($currentPage <= $pages) {
    sleep(0.1);
    flush();
    $browseNodeLookup->setPage($currentPage);

    try {
        $xml = $apaiIO->runOperation($browseNodeLookup);
        $all_elements =  iq_parse_data($xml, $itemcount, $all_elements);
    } finally {
        $currentPage++;
    }
}
while($currentPage setPage($currentPage);
试一试{
$xml=$apaiIO->runOperation($browseNodeLookup);
$all_elements=iq_parse_数据($xml、$itemcount、$all_elements);
}最后{
$currentPage++;
}
}