File_get_contents high latency usage, file_get_contents

Source: Internet
Author: User

File_get_contents high latency usage, file_get_contents

First, solve the super-time problem of file_get_contents. After the super-time is returned, perform a batch commit like settimeout in js, after three or five attempts, the system confirmed that the server could not be accessed, and the server was placed on the bottom.
Here we will introduce two solutions:

I. added time limit

Note: set_time_limit is only used to set the expiration time of your PHP program, rather than the expiration time of the URL for file_get_contents function.
At first, I thought that set_time_limit could also affect file_get_contents. Later, it was invalid. The real modification of file_get_contents can be extended using the timeout metric data of resource $ context:
PHP program example

$ Opts = array (
'Http' => array (
'Method' => "GET ",
'Timeout' => 60,
)
);

$ Context = stream_context_create ($ opts );

$ Html = file_get_contents ('HTTP: // www.example.com ', false, $ context );
Fpassthru ($ fp );

Ii. repeated renewal

PHP program example
$ Cnt = 0;
While ($ cnt <3 & ($ str = @ file_get_contents ('HTTP... ') === FALSE ){
$ Cnt ++;
}

The above method is OK for the payment process. Next, we will demonstrate how to use file_get_contents to implement Post, as shown below:
PHP program example

Function Post ($ url, $ post = null ){
$ Context = array ();
If (is_array ($ post )){
Ksort ($ post );

$ Context ['http'] = array (
'Timeout' => 60,
'Method' => 'post ',
'Content' => http_build_query ($ post ,'','&'),
);
}

Return file_get_contents ($ url, false, stream_context_create ($ context ));
}

$ Data = array (
'Name' => 'test ',
'Email '=> 'test @ gmail.com ',
'Submit '=> 'submit ',
);

Echo Post ('HTTP: // www.example.com ', $ data );

Note that the Set_time_out at the beginning of the case should be too late for the entire case.


File_get_contents usage

File_get_contents-read the entire file into a string

Description
String file_get_contents (string $ filename [, bool $ use_include_path [, resource $ context [, int $ offset [, int $ maxlen])
Like file (), only file_get_contents () can read the file into a string. The content with the length of maxlen will be read at the position specified by the offset parameter. If it fails, file_get_contents () returns FALSE.

The file_get_contents () function is used to read the file content into a string. If the operating system supports it, the memory ing technology will be used to enhance the performance.

Note: If you want to open a URL with special characters (such as spaces), you need to use urlencode () for URL encoding.

Note: The context parameter can be ignored using NULL.

How to Use file_get_contents of php

File_get_contents () reads the content of a file, including the remote file !!
The file_get_contents () function is used to read the file content into a string !!
<? Php
Echo file_get_contents ('www .baidu.com ');
?>

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.