Return: Details of file_get_contents timeout handling

  • 2020-06-19 09:57:06
  • OfStack

The weather cleared up at last, but there was a problem. When implementing user data synchronization between two sites, when using the php function file_get_contents to fetch and execute remote pages, if the connection time out will output 1 Fatal Error or rather slow, the following code will not run as a result. Start with the PHP file_get_contents() function
Definition and usage
The file_get_contents() function reads the entire file into a string.
Unlike file() 1, file_get_contents() reads the file into a string.
The file_get_contents() function is the preferred method used to read the contents of the file into a string. Memory mapping is also used to enhance performance if the operating system supports it.
grammar
file_get_contents (path include_path, context start, max_length) parameter description
path required. Specifies the file to read.
include_path optional. If you also want to search for files in include_path, you can set this parameter to "1".
context optional. Specifies the environment for the file handle.
context is a set of options that can modify the behavior of a stream. If null is used, it is ignored.
start optional. Specifies where to start reading in a file. This parameter is new to PHP 5.1.
max_length optional. Specifies the number of bytes to read. This parameter is new to PHP 5.1.
instructions
Support for context was added to PHP 5.0.0.
There are generally two solutions for timeouts or pages that are too slow:

1. Use the third parameter of file_get_contents()

$url = "http://zhoz.com/zhoz.php";      
$ctx = stream_context_create(array(      
 ' http' => array( ' timeout' => 10)      
    )      
    );      
$result = @file_get_contents($url, 0, $ctx);      
if($result){      
        var_dump($result);      
    }else{      
echo " Buffer is empty";      
    }      
?>   

This method 1, I tested in the local response is good, but if the external network test (environment: China → between servers in the United States) is basically a timeout situation.
Testing TimeOut is basically useless, suggest the following way

2. Use the curl extension library

$url = "http://zhoz.com/zhoz.php";      
try {      
echo date( ' Y-m-d h:i:s');      
echo "";      
//$buffer = file_get_contents($url);    
$buffer = zhoz_get_contents($url);      
echo date( ' Y-m-d h:i:s');      
if(emptyempty($buffer)) {      
echo " Buffer is empty";      
        } else {      
echo " Buffer is not empty";      
        }      
    } catch(Exception $e) {      
echo "error ";      
    }      
function zhoz_get_contents($url, $second = 5) {      
$ch = curl_init();      
        curl_setopt($ch,CURLOPT_URL,$url);      
        curl_setopt($ch,CURLOPT_HEADER,0);      
        curl_setopt($ch,CURLOPT_TIMEOUT,$second);      
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);      
$content = curl_exec($ch);      
        curl_close($ch);      
return $content;      
    }      
?> 

Review and choose which method to apply according to the system environment:

function vita_get_url_content($url) {   
if(function_exists( ' file_get_contents')) {   
$file_contents = file_get_contents($url);   
} else {   
$ch = curl_init();   
$timeout = 5;   
curl_setopt ($ch, CURLOPT_URL, $url);   
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   
$file_contents = curl_exec($ch);   
curl_close($ch);   
}   
return $file_contents;   
}   
?>  

Related articles: