php PHP循环curl请求一一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13257814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP loop curl request one by one
提问by Osa
How to make a foreachor a forloop to run only when the curl response is received..
如何使一个foreach或一个for循环仅在收到 curl 响应时运行..
as example :
例如:
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
}
i don't want it to move to the next loop without having the current curl request data received.. one by one, so basically it opens up the url at first time, waits for the request data, if something matched or came true then go to the next loop,
我不希望它在没有收到当前 curl 请求数据的情况下移动到下一个循环。进入下一个循环,
you dont have to be bothered about the 'curl' part, i just want the loop to move one by one ( giving it a specific condition or something ) and not all at once
您不必担心“卷曲”部分,我只想让循环一个一个地移动(给它一个特定的条件或其他东西)而不是一次移动
回答by LSerni
The loop ought to already work that way, for you're using the blocking cURL interface and not the cURL Multi interface.
循环应该已经这样工作了,因为您使用的是阻塞 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);
回答by endorama
The continuecontrol structure should be what you are looking for:
该continue控制结构应该是你在找什么:
continueis used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.
continue在循环结构中用于跳过当前循环迭代的其余部分,并在条件评估和下一次迭代开始时继续执行。
http://php.net/manual/en/control-structures.continue.php
http://php.net/manual/en/control-structures.continue.php
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
continue; // ?? - go to the next loop
}
// DONT go to the next loop until the above data is complete or returns true
}
回答by Nikolaos Dimopoulos
Your code (as is) will not move to the next iteration until the curlcall is completed.
在curl调用完成之前,您的代码(按原样)不会移动到下一次迭代。
A couple of issues to consider
需要考虑的几个问题
- You could set a higher timeout for
curlto ensure that there are no communication delays.CURLOPT_CONNECTTIMEOUT,CURLOPT_CONNECTTIMEOUT_MS(milliseconds),CURLOPT_DNS_CACHE_TIMEOUT,CURLOPT_TIMEOUTandCURLOPT_TIMEOUT_MS(milliseconds) can be used to increase the timeouts. 0 makescurlwait indefinitely for any of these timeouts. - If your
curlrequest fails for whatever reason, you can just put anexitthere to stop execution, This way it will not move to the next URL. - If you want the script to continue even after the first failure, you can just log the result (after the failed request) and let it continue in the loop. Examining the log file will give you information as to what happened.
- 您可以设置更高的超时时间
curl以确保没有通信延迟。CURLOPT_CONNECTTIMEOUT,CURLOPT_CONNECTTIMEOUT_MS(毫秒),CURLOPT_DNS_CACHE_TIMEOUT,CURLOPT_TIMEOUT和CURLOPT_TIMEOUT_MS(毫秒) 可用于增加超时。0curl无限期地等待任何这些超时。 - 如果您的
curl请求由于某种原因失败,您可以在exit那里放置一个停止执行,这样它就不会移动到下一个 URL。 - 如果您希望脚本在第一次失败后继续运行,您可以只记录结果(在请求失败后)并让它继续循环。检查日志文件将为您提供有关发生了什么的信息。
回答by Alex Howansky
You can break out of a loop with the breakkeyword:
您可以使用break关键字跳出循环:
foreach ($list as $thing) {
if ($success) {
// ...
} else {
break;
}
}
回答by Enes Tanr??ver
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
continue;
}else{
break;
}
// DONT go to the next loop until the above data is complete or returns true
}
or
或者
for($i = 1; $i <= 10; $i++) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://www.example.com");
if(curl_exec($ch)===false){ // ?? - if request and data are completely received
break;
}
}

