Example of file_get_contents high-latency usage in PHP, filegetcontents

Source: Internet
Author: User
Tags learn php programming

Example of file_get_contents high-latency usage in PHP, filegetcontents

This article describes the advanced usage of file_get_contents in PHP. The specific analysis is as follows:

First, solve the problem of file_get_contents timeout. After a timeout error is returned, an attempt is made like settimeout in js. After the error is returned three or five times, the attempt is confirmed to be unable to connect to the server and the attempt is completely abandoned.
Here are two solutions:

I. added the timeout time limit.

Note: set_time_limit only sets the timeout time for your PHP program, rather than the timeout time for the file_get_contents function to read the URL.

At first, I thought that set_time_limit could also affect file_get_contents. Later, it was tested to be invalid. You can use the timeout parameter of resource $ context to modify the file_get_contents latency:

The PHP code is as follows:

$opts = array(    'http'=>array(      'method'=>"GET",      'timeout'=>60,    ));$context = stream_context_create($opts);$html =file_get_contents('http://www.bkjia.com', false, $context);fpassthru($fp);

2. multiple attempts

The PHP code is as follows:

$cnt=0;while($cnt < 3 && ($str=@file_get_contents('http...'))===FALSE){   $cnt++;}

The above method has timed out. Next we will demonstrate how to use file_get_contents to implement Post, as shown below:
PHP code

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.bkjia.com', $data);

Note that the Set_time_out in the document header is invalid. Otherwise, the entire document has timed out.

I hope this article will help you learn php programming.


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 ');
?>

Question about file_get_contents 3rd parameters in php

This context refers to "content in the stream ".
We know that all OS operations are controlled by input and output streams.
Enable some special files (everything in unix is a file), such as devices, keyboards, screens, network files, and serial ports.
You need to input a stream for some operations. In this case, use stream_context_create to create an "input content ".

In actual use, we can see that most instances are also used for HTTP operations (the actual use is far from limited to this function)
The content of the HTTP Header is used as the content of the input stream. In this way, the file is opened and the content returned by the service is obtained.
In principle, the system file input and output streams are also operated.

<? Php
$ Opts = array (
'Http' => array (
'Method' => "GET ",
'Header' => "Accept-language: en \ r \ n ".
"Cookie: foo = bar \ r \ n"
)
);
$ Context = stream_context_create ($ opts );
// Submit the language code and cookie to obtain the response content of a website.
$ File = file_get_contents ('www .example.com/', false, $ context );
?>

I hope it will not be a tragedy. I have been unable to understand it for a long time.

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.