【问题标题】:If function execute more than x seconds php如果函数执行超过 x 秒 php
【发布时间】:2013-07-23 23:07:20
【问题描述】:

在我的脚本中我使用了这个函数:

function url_exists($url) {
 if ((strpos($url, "http")) === false) $url = "http://" . $url;
 if (is_array(@get_headers($url)))
      return true;
 else
      return false;
}

如何设置执行函数@get_headers 的时间限制?我需要像 set_time_limit() 这样的函数,它适用于一个函数,而不是整个脚本。

【问题讨论】:

  • 您是否安装了curl 扩展程序?

标签: php time set limit get-headers


【解决方案1】:

函数get_headers() 不支持超时或上下文参数。试试:

ini_set('default_socket_timeout', YOUR_VALUE_IN_SECONDS);

这会将默认超时设置为您选择的值。操作完成后不要忘记重置超时。

【讨论】:

    【解决方案2】:

    您可以使用ini setting 或通过为流创建默认上下文值来设置它:

    stream_context_set_default(array(
        'http' => array('timeout' => 10) // in seconds
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多