File_get_contents high latency usage, file_get_contents
First, solve the super-time problem of file_get_contents. After the super-time is returned, perform a batch commit like settimeout in js, after three or five attempts, the system confirmed that the server could not be accessed, and the server was placed on the bottom.
Here we will introduce two solutions:
I. added time limit
Note: set_time_limit is only used to set the expiration time of your PHP program, rather than the expiration time of the URL for file_get_contents function.
At first, I thought that set_time_limit could also affect file_get_contents. Later, it was invalid. The real modification of file_get_contents can be extended using the timeout metric data of resource $ context:
PHP program example
$ Opts = array (
'Http' => array (
'Method' => "GET ",
'Timeout' => 60,
)
);
$ Context = stream_context_create ($ opts );
$ Html = file_get_contents ('HTTP: // www.example.com ', false, $ context );
Fpassthru ($ fp );
Ii. repeated renewal
PHP program example
$ Cnt = 0;
While ($ cnt <3 & ($ str = @ file_get_contents ('HTTP... ') === FALSE ){
$ Cnt ++;
}
The above method is OK for the payment process. Next, we will demonstrate how to use file_get_contents to implement Post, as shown below:
PHP program example
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 '=> 'test @ gmail.com ',
'Submit '=> 'submit ',
);
Echo Post ('HTTP: // www.example.com ', $ data );
Note that the Set_time_out at the beginning of the case should be too late for the entire case.
File_get_contents usage
File_get_contents-read the entire file into a string
Description
String file_get_contents (string $ filename [, bool $ use_include_path [, resource $ context [, int $ offset [, int $ maxlen])
Like file (), only file_get_contents () can read the file into a string. The content with the length of maxlen will be read at the position specified by the offset parameter. If it fails, file_get_contents () returns FALSE.
The file_get_contents () function is used to read the file content into a string. If the operating system supports it, the memory ing technology will be used to enhance the performance.
Note: If you want to open a URL with special characters (such as spaces), you need to use urlencode () for URL encoding.
Note: The context parameter can be ignored using NULL.
How to Use file_get_contents of php
File_get_contents () reads the content of a file, including the remote file !!
The file_get_contents () function is used to read the file content into a string !!
<? Php
Echo file_get_contents ('www .baidu.com ');
?>