【问题标题】:avoiding network issues between cURL calls避免 cURL 调用之间的网络问题
【发布时间】:2016-07-11 06:07:46
【问题描述】:

我正在尝试包含两个 cURL 命令块。第二个块取决于第一个块返回的值。我知道在尝试在服务器之间建立 cURL 调用时可能存在网络问题。第一个块返回的值将保存在数据库中。我想知道的是如何在 PHP 文件上编写将执行两个 cURL 命令块的语法,这样当第一个块返回值时,我可以启动第二个块,记住可能存在网络问题.我知道如何编写 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 中使用 // 吗?

谢谢

【问题讨论】:

  • //是正确的,也可以使用/*...*/
  • 您无法阻止网络问题,因为这不在您的控制范围内。您只能对网络连接问题做出反应。 phps cURL 扩展提供了查询其返回值和错误消息的方法,只需查看文档即可。

标签: php mysql curl server


【解决方案1】:

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;

【讨论】:

    猜你喜欢
    • 2011-12-02
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多