Am正在尝试包含两个cURL命令块。第二个块取决于第一个块返回的值。我知道尝试在服务器之间建立cURL调用可能存在网络问题。第一个块返回的值将保存在数据库中。我想知道的是如何在将执行两个cURL命令块的PHP文件上编写语法,以便当第一个块返回值时,我可以启动第二个块,记住可能存在网络问题。我知道如何编写cURL命令块。我只需要避免网络问题的部分。以下是我的想法:(在PHP文件中)
$default = "default value";
$a = $default;
//first block executes initializing $a
$a = "value returned by first block. Already saved to database";
//second block can't execute until this value is not set to default value
while($a != $default){
//second block executes
//somewhere to the end of second block
$result = curl_exec($ch); //where $ch is the handle for my cURL call
//exiting the while loop:
if(isset($result))exit(0); //don't know if this is the proper way of exit
//a while loop inside a PHP script
}
请告诉我这是否正确,或者我如何改进它。还有一件事,因为我对PHP还很陌生,你如何评论PHP中的语法行?你在JAVA中使用//吗?
感谢
cURL不是异步的,所以您不必担心让代码等待它完成。也就是说,在cURL请求完成之前,PHP不会执行curl_exec()
之后的行。像这样的东西应该对你有用:
$result = curl_exec($ch);
if($result !== false){
//Do Stuff on successful curl
}
else{
echo curl_error($ch); //print the curl error
}
此外,退出while循环的方法是break;
,如果您只想跳过当前迭代,请使用continue;