我有一段相当简单的PHP代码
$xml = file_get_contents($url, false, $context);
$response = simplexml_load_string($xml);
$status_response = $response->xpath('/PollSessionResponseDto/Status');
$answer = $status_response[0];
while ($answer<>"UpdatesComplete") {
sleep(2);
$answer = $response->xpath('/PollSessionResponseDto/Status')[0];
}
$itenaries = $response->xpath('/PollSessionResponseDto/Itineraries/ItineraryApiDto');
例如,我正在读取一个XML文件,并且只有当XML文件中的状态为"UpdatesComplete"时,我才想继续读取条目。如果状态是"UpdatesPending",我基本上想等待2秒。然后再检查一次,如有必要,再等2秒。
但是我最终进入了一个无限循环。我检查了我的$answer
变量,似乎我正在获得我想要检查的字符串(UpdatesPending或UpdatesComplete)。
$answer
的值将永远不会在循环中改变,因为您永远不会从远程主机重新加载XML文件。
do {
$xml = file_get_contents($url, false, $context);
$response = simplexml_load_string($xml);
$answer = $response->xpath('/PollSessionResponseDto/Status')[0];
} while ($answer != "UpdatesComplete" && slee(2));