Osa*_*Osa 5 php foreach curl loops for-loop
仅在收到curl响应时如何使foreach或for循环运行。
例如:
for ($i = 1; $i <= 10; $i++) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://www.example.com");
if(curl_exec($ch)){ // ?? - if request and data are completely received
// ?? - go to the next loop
}
// DONT go to the next loop until the above data is complete or returns true
}
Run Code Online (Sandbox Code Playgroud)
我不想让它移到下一个循环而又没有收到当前的curl请求数据..一个接一个,所以基本上它第一次打开url,等待请求数据,如果有匹配或实现了,那么转到下一个循环,
您不必为“卷曲”部分而烦恼,我只希望循环逐个移动(给定特定条件或某些条件)而不是一次全部移动
循环应该已经这样工作了,因为您正在使用阻塞的cURL接口,而不是cURL Multi接口。
$ch = curl_init();
for ($i = 1; $i <= 10; $i++)
{
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
$res = curl_exec($ch);
// Code checking $res is not false, or, if you returned the page
// into $res, code to check $res is as expected
// If you're here, cURL call completed. To know if successfully or not,
// check $res or the cURL error status.
// Removing the examples below, this code will hit always the same site
// ten times, one after the other.
// Example
if (something is wrong, e.g. False === $res)
continue; // Continue with the next iteration
Here extra code to be executed if call was *successful*
// A different example
if (something is wrong)
break; // exit the loop immediately, aborting the next iterations
sleep(1); // Wait 1 second before retrying
}
curl_close($ch);
Run Code Online (Sandbox Code Playgroud)
您可以使用关键字打破循环break:
foreach ($list as $thing) {
if ($success) {
// ...
} else {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18427 次 |
| 最近记录: |