PHP file_get_contents sets the timeout handling method

  • 2020-10-23 20:03:54
  • OfStack

Timeout handling for file_get_contents

In other words, starting with PHP5, file_get_content already supports context (the manual says: 5.0.0 Added the context support.) that is, starting with 5.0, file_get_contents can also support POST data.

Today's post is about timeouts, and it's true that when you commit across servers, you inevitably run into timeouts. What do you do then? set_time_limit is not useful, only controlled with timeout time in context. Instead, we're not going to stop, we're going to manage. For example, after the timeout returns an error, make a try, just like settimeout in js, to reprocess the function. After three or five errors, we actually decided we couldn't connect to the server and gave up. This is a good method and should be recommended. In fact. file_get_contents not all file_get_contents, as long as support context should be added, to avoid time-out waste time. Some of the functions that can be supported in this way are: fsocketopen (the last parameter of the function). stream_time_out), fopen (also supported from PHP5), file(supported from PHP5),curl (curl has its own variable CURLOPT_TIMEOUT), etc.

In the use of file_get_contents function, there is often a timeout situation, here to look at the error prompt 1, see what kind of error, more common is read super, this situation you can try to avoid or solve by 1 of the methods. Here are two:

1. Increase the timeout limit

Note here: set_time_limit only sets the timeout of your PHP program, not the file_get_contents function to read the timeout of URL.

At first, I thought that set_time_limit could also affect file_get_contents, but it was invalid after testing. Real modification of file_get_contents delay can be done using the timeout parameter of resource $context:


$opts = array(   
  'http'=>array(   
    'method'=>"GET",   
    'timeout'=>1,// Unit s   
   )   
);    

 $cnt=0;   
while($cnt<3 && ($bb=file_get_contents("https://www.ofstack.com", false, stream_context_create($opts)))===FALSE) $cnt++;   
echo $cnt;   
echo $bb;  

2. If there is a delay, try it several times

Sometimes the failure is caused by network and other factors, but there is no solution, but you can modify the program, if the failure retries several times, you still give up after failure, because file_get_contents() will return to FALSE if the failure, so you can write the code as follows:


$cnt=0;   
 while($cnt<3 && ($bb=file_get_contents("https://www.ofstack.com", false, stream_context_create($opts)))===FALSE) $cnt++;

The above method to deal with timeout has OK. What about Post? Be careful, someone found 'method'= > "GET", right! Can I set it to post? Baidu looked for the relevant information, it is also true! And someone has written a copycat version of the post pass-value function, as follows:


function Post($url, $post = null){   
    $context = array ();   
    if (is_array ( $post )) {   
        ksort ( $post );   
        $context ['http'] = array (   
            'timeout' => 60,    
            'method' => 'POST',    
            'content' => http_build_query( $post, '', '&' )   
         );   

    }   
    return file_get_contents ( $url, false, stream_context_create ( $context ) );   
}   

 $data = array (   
    'name' => 'test',   
    'email' => 'admin@admin.com',   
    'submit' => 'submit',   
);   
echo Post ( 'https://www.ofstack.com', $data );  

OK, the above function is perfect, which solves both timeout control and Post value passing.


Related articles: