我正在尝试包含两个 cURL 命令 block 。第二个 block 取决于第一个 block 返回的值。我知道尝试在服务器之间建立 cURL 调用时可能会出现网络问题。第一个 block 返回的值将保存在数据库中。我想知道的是如何在 PHP 文件上编写将执行两个 cURL 命令 block 的语法,以便当第一个 block 返回值时我可以启动第二个 block ,请记住可能存在网络问题。我知道如何编写 cURL 命令 block 。我只需要能够避免网络问题的部分。这是我的想法:(在 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;
关于php - 避免 cURL 调用之间的网络问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36186835/