Execute, get remote code return: file_get_contents The problem of timeout processing _php tutorial

Source: Internet
Author: User
Tags echo date
The weather finally cleared up, but the problem came. In the implementation of user data synchronization between two sites, when using PHP function file_get_contents Crawl Execution Remote page, if the connection timeout will output a fatal error or rather slow, resulting in the following code will not run. Check out the PHP file_get_contents () function first
Definition and usage
The file_get_contents () function reads the entire file into a string.
As with file (), the difference is that file_get_contents () reads a file into a string.
The file_get_contents () function is the preferred method for reading the contents of a file into a string. If supported by the operating system, memory-mapping techniques are also used to enhance performance.
Grammar
File_get_contents (path,include_path,context,start,max_length) parameter description
Path is required. Specifies the file to be read.
Include_path is optional. If you also want to search for files in include_path, you can set this parameter to "1".
The context is optional. Specifies the environment for file handles.
The context is a set of options that modify the behavior of the stream. If NULL is used, it is ignored.
Start is optional. Specifies the location to begin reading in the file. This parameter is new in PHP 5.1.
Max_length is optional. Specifies the number of bytes to read. This parameter is new in PHP 5.1.
Description
Support for the context is added by PHP 5.0.0.
For timeouts or pages that are too slow, you can typically take two solutions:

I. Use of file_get_contents () the third parameter
Copy CodeThe code is as follows:
$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 reflected well, but if the external network test (environment: China → United States server) is basically a case of timeouts.
The test of timeout is basically useless, the following methods are recommended

Two. Extending the library with Curl
Copy CodeThe code is as follows:
$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, according to the system environment to choose exactly which method to apply:
Copy CodeThe code is as follows:
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;
}
?>

http://www.bkjia.com/PHPjc/327837.html www.bkjia.com true http://www.bkjia.com/PHPjc/327837.html techarticle The weather finally cleared up, but the problem came. In the implementation of user data synchronization between two sites, when using PHP function file_get_contents crawl to execute a remote page, if the connection timeout will output a ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.