Execute, get remote code return: File

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 the use of PHP functions file_get_contents Crawl execution of remote pages, if the connection timeout will output a fatal error or rather slow, resulting in the following code can not run. Let's take a look at the PHP file_get_contents () function
Definitions 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 the file into a string.
The file_get_contents () function is the preferred method for reading the contents of a file into a string. Memory mapping technology is also used to enhance performance if supported by the operating system.
Grammar
file_get_contents (path,include_path,context,start,max_length) parameter description
Path required. Specify the files to read.
Include_path Optional. If you also want to search for files in include_path, you can set this argument to "1".
The context is optional. The environment that prescribes the file handle.
The context is a set of options that can modify the behavior of a stream. If NULL is used, it is ignored.
Start optional. Specify where to start reading in the file. This parameter is a new addition to PHP 5.1.
Max_length Optional. Specify the number of bytes read. This parameter is a new addition to PHP 5.1.
description
the support for the context is added by PHP 5.0.0.
For timeouts or pages that are slow, you can generally take two solutions:

I. Using file_get_contents () third parameter

Copy Code code 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 reflection is good, but if the external network test (environment: China → United States server) is basically timeout situation.
Testing the timeout is basically useless, suggest the following ways

Two. Using the Curl Extension library

Copy Code code 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 Code code 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;
}

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.