Execute and obtain remote code and return: file_get_contents timeout handling problem details _ PHP Tutorial

Source: Internet
Author: User
Execute and obtain the remote code and return the following details: file_get_contents timeout handling. The weather has finally cleared up, but the problem has come. To synchronize user data between two sites, when you use the php function file_get_contents to capture and execute a remote page, if the connection times out, the weather will eventually become fine, but the problem will arise. To synchronize user data between two sites, when you use the php function file_get_contents to capture and execute remote pages, if the connection times out, a Fatal Error or a considerable slowness will be output, as a result, the following code cannot be run. First, let's take a look at the PHP file_get_contents () function.
Definition and usage
The file_get_contents () function reads the entire file into a string.
Like 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 file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.
Syntax
File_get_contents (path, include_path, context, start, max_length) parameter description
Path is required. Specifies the file to be read.
Optional. If you want to search for files in include_path, you can set this parameter to "1 ".
Context is optional. Specifies the file handle environment.
Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored.
Start is optional. Specifies the location where the file starts reading. This parameter is newly added in PHP 5.1.
Max_length is optional. The number of bytes to read. This parameter is newly added in PHP 5.1.
Description
The support for context is added in PHP 5.0.0.
There are two solutions for timeout or slow pages:

1. use the third parameter file_get_contents ()

The 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 it locally, but if it is tested on the Internet (Environment: China → U.S. server room), it usually times out.
The TimeOut test is useless. the following method is recommended:

II. use curl Extension Library

The 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;
}
?>


In summary, which method is used based on the system environment:

The 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;
}
?>

Bytes. To synchronize user data between two sites, when you use the php function file_get_contents to capture and execute remote pages, if the connection times out, a request will be output...

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.