Using Curl
You can use curl to implement a curl_file_get_contents function yourself
Curlopt_followlocation TRUE will redirect "Location:" in the HTTP header based on server return. (Note: This is recursive, "Location:" several times to redirect, unless the curlopt_maxredirs is set, limit the maximum number of redirects
function curl_file_get_contents ($durl) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $durl);
curl_setopt ($ch, Curlopt_returntransfer, 1); Returns the obtained information as a string
curl_setopt ($ch, curlopt_followlocation, 1);
curl_setopt ($ch, Curlopt_maxredirs, 5); Specifies the maximum number of HTTP redirects
curl_setopt ($ch, curlopt_timeout,2);//allow the maximum number of seconds that the Curl function executes
$r = curl_exec ($ch);
if (Curl_errno ($ch)) { //If there is an error, the output error (timeout does not throw an exception, you need to use this function to set their own processing)
echo ' Curl error: '. Curl_error ($ch);
} return
$r;
}
Timeout error output similar to the bottom
Curl error:operation timed out after 991 milliseconds and 3706978 out of 155372766 bytes
Other possible ways
Set maximum execution time for a script (not valid)
void Set_time_limit (int $seconds)
If this setting is exceeded, the script returns a fatal error. The default value is 30 seconds, or the value defined in PHP.ini's max_execution_time, if this value exists. If set to 0 (0), there is no time limit.
When this function is invoked, Set_time_limit () Restarts the timeout counter from zero. In other words, if the timeout defaults to 30 seconds and calls Set_time_limit (20) when the script runs for 25 seconds, the script can run for a total of 45 seconds before timing out.
Warning
This feature is not valid when PHP is running in safe mode. There is no other way than to turn off safe mode or change the time limit in php.ini.
Note:
The Set_time_limit () function and the configuration instruction Max_execution_time only affect the time that the script itself executes. Any execution of a script that occurs in a system call, such as a systems (), a stream operation, a database operation, and so on, is not included when the script is run. This is not the case in Windows, where the measurement time is real.
Because File_get_contents is a stream operation, this cannot be limited.
Default_socket_timeout (Invalid)
File_get_contents Timeout time in PHP
Max_execution_time = 30
Default_socket_timeout = 60
Suppose you use file_get_contents to spend 45, and Max_execution_time is 30, will it timeout?
The answer is no, because Max_execution_time does not affect operating system calls or stream operations
Default_socket_timeout is calculated before the socket response, and will be executed as long as the response is received.
Using the HTTP context option (not valid)
HTTP Context Options
It is estimated that the timeout here does not refer to the execution time of the entire function on the surface or the invalid
$opts = Array ('
http ' =>array (
' method ' => ' get ',
' timeout ' =>60,
)
);
$context = Stream_context_create ($opts);
$html =file_get_contents (' http://www.example.com ', false, $context);
The above is the entire content of this article, I hope to help you, I hope you will continue to pay attention to the latest content of the cloud-dwelling community.