【问题标题】:file_get_contents() Timeout in phpphp 中的 file_get_contents() 超时
【发布时间】:2016-10-10 03:51:31
【问题描述】:

我知道 php 不提供超时选项,因为它是从服务器端运行的。但我的逻辑需要超时。请解决这个问题

我的 PHP: login.php :-

$forms_values = $_POST;
$url = "http://sample.com/sample?params=$forms_values";
$resp = file_get_contents($url);
// It takes much time if sample.com is down.
// so need to set time out here.
$response = json_decode($resp, true);
....
....
if ($response["auth"] == "yes"){
   header("index.php");
}

我的 html index.html :-

<form action="login.php" method="post" id="clientLogin">
    <div>
        <input id="username" placeholder="Email or Username" type="text" name="luser" size="30" value="">
    </div>
    <div>
        <input id="passwd" placeholder="Password" type="password" name="lpasswd" size="30" value="">
    </div>
<input class="btn" type="submit" value="Sign In">
</form>
<script type="text/javascript">
//This is not working. and obviously delay will happen in server code 
//and i cant get it in client side 

$(window).load(function () {
     var submit = false;
     $("#clientLogin").submit(function(e) {
         alert("me after 1000 mili seconds");
          setTimeout(function(){
              alert("me after 1000 mili seconds");
              submit = true;
              $("#clientLogin").submit(); // if you want            
          }, 15000);
          if(!submit)
              e.preventDefault();
     });
};
</script>

此 javascript 无法正常工作。显然延迟会发生在服务器代码中,我无法从客户端观看它。

所以这就是我想要的。如果我的file_get_contents() 函数长时间没有响应,需要终止提交过程(从提交到渲染下一页)。

注意: 请不要让我使用 curl,因为我被指示不要使用 curl。 我无法在客户端获取它

【问题讨论】:

    标签: javascript php timeout file-get-contents


    【解决方案1】:

    如果您在 5 秒内没有收到内容,$resp 将为空

    
    $ctx = stream_context_create(['http'=> ['timeout' => 5]]); // 5 seconds
    $resp = file_get_contents($url,null,$ctx);
    
    

    【讨论】:

      【解决方案2】:

      set_time_limit() 确实全局运行,但可以在本地重置。

      设置允许脚本运行的秒数。如果达到此值,脚本将返回致命错误。默认限制为 30 秒,如果存在,则为 php.ini 中定义的 max_execution_time 值。

      调用时,set_time_limit() 会从零重新启动超时计数器。换句话说,如果超时是默认的 30 秒,并且在脚本执行 25 秒后调用了诸如 set_time_limit(20) 之类的调用,则脚本将在超时之前总共运行 45 秒。 我没有测试过,但是你可以在本地设置,离开时重置

      <?php
      set_time_limit(0);  // global setting
      
      function doStuff()
      {
          set_time_limit(10);   // limit this function
          //  stuff
          set_time_limit(10);   // give ourselves another 10 seconds if we want
          //  stuff
          set_time_limit(0);    // the rest of the file can run forever
      }
      
      // ....
      sleep(900);
      // ....
      doStuff();  // only has 10 secs to run
      // ....
      sleep(900);
      // ....
      

      【讨论】:

        【解决方案3】:

        或者,考虑使用 cURL

        它提供了 CURLOPT_CONNECTTIMEOUT 参数来指定尝试建立连接时要等待的时间量。

        【讨论】:

        • 一切正常,ini_set('default_socket_timeout', 2); 一个人痊愈了。谢谢你的回答,我很感激。
        【解决方案4】:

        将代码放在脚本的开头

        ini_set('default_socket_timeout', 30); // 30 seconds
        

        默认设置为 60 秒。

        【讨论】:

        • 感谢您的回答我很感激
        【解决方案5】:

        file_get_contents() 中超时的默认值为 60 秒。 您可以通过 default_socket_timeout 设置更改此值。 它在您的代码中也是可行的:

        ini_set('default_socket_timeout', 2); // 2 seconds
        

        【讨论】:

        • 如果我的file_get_contents() 超时,它将返回什么。因为我需要向客户端发送一些错误消息。
        • 它应该返回 FALSE。使用 === 运算符比较返回值
        • 它有效。感谢ini_set()。并在我的警告消息旁边设置ini_set('default_socket_timeout', 60); 将使其成为默认(60 秒)仪式?
        • 是的。如果你想重置它,你会这样做。
        猜你喜欢
        • 1970-01-01
        • 2011-04-11
        • 2014-05-09
        • 2015-07-02
        • 2012-04-28
        • 1970-01-01
        • 2013-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多