简体   繁体   中英

If function execute more than x seconds php

In my script i use this function:

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

How can i set the time limit of execute function @get_headers? I need function like set_time_limit() by works for one function, not for whole script.

The function get_headers() doesn't supports a timeout or a context param. Try:

ini_set('default_socket_timeout', YOUR_VALUE_IN_SECONDS);

This will set the default timeout to a value of your choice. Don't forget to reset the timeout after the operation has finished.

You can set it with ini setting or by creating default context values for streams:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM