PHP Stop script file_get_contents () after 2 seconds

So, I have a PHP script that loads another page using file_get_contents().

On another page, it takes about 30 seconds (the type of script is this), and all I need is file_get_contents()to run this script.

The problem is that it file_get_contents()will remain loaded for 30 seconds. The only way to stop this is to close the tab (when you close the script tab, it still works).

So how can I get it to file_get_contents();close after two seconds?

Thanks in advance

+5
source share
3 answers

It is not possible to answer your question, but I recommend using cURL instead file_get_contents.

Example:

function get_url_contents($url) {
    $crl = curl_init();

    curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
    curl_setopt($crl, CURLOPT_URL, $url);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);

    $ret = curl_exec($crl);
    curl_close($crl);
    return $ret;
}

: default_socket_timeout

, 2 , , - 2 . - 60 .

+3

, file_get_contents() , $context, . , HTTP, timeout :

float

Read timeout in seconds, specified by a float (e.g. 10.5).

By default the default_socket_timeout setting is used.

, , default_socket_timeout ( ).

: .

+1

, set_time_limit (2) file_get_contents.

0

All Articles