Stream_context_create solves file_get_contents timeout

Source: Internet
Author: User

Stream_context_create:
Creates and returns a text data stream and applies various options. It can be used in special procedures such as timeout setting, proxy server, request method, and header information setting for fopen () and file_get_contents.

Function prototype: Resource stream_context_create ([array $ options
[, Array $ Params])

When using the file_get_contents function, timeout often occurs. Here, you need to check the error message to see which error occurs. The more common is read timeout, in this case, you can try to avoid or solve the problem. Here we will briefly introduce two types:

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. I
It was initially thought that set_time_limit could also affect file_get_contents. Later, it was tested to be invalid. Real Modification
For file_get_contents latency, you can use resource
$ Context's timeout parameter:
 

$ Opts = array (

'Http' => array (

'Method' => "get ",

'Timeout' => 60,
)
);
// Create a data stream Context
$ Context = stream_context_create ($ opts );

$ Html = file_get_contents ('HTTP: // blog.sina.com/mirze', false,
$ Context );

// All the remaining data at the pointer of the fopen output file:
// Fpassthru ($ FP); // used before fclose ()

2. Try multiple times if there is a latency

Sometimes the failure is caused by factors such as the network, there is no solution, but you can modify the program, retry several times when the failure is still failed, give up, because file_get_contents () if the failure will return
False, so you can write the code as follows:
$ CNT = 0;
While ($ CNT <3 &&
($ STR = @ file_get_contents ('HTTP: // blog.sina.com/mirze') = false)
$ CNT ++;

The above method has timed out.

What about post? Be careful when someone finds 'method' => "get ",
Yes! Can it be set to post? Baidu has found the relevant information. You can do it! In addition, someone wrote the post value passing function in the shanzhai version, as shown below:

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

OK,
The above function is perfect, both timeout control and post value passing are solved. In combination with kangsheng's improved version of RC4 encryption and decryption algorithm, it is much easier to create a secure WebService.

Reference: http://hi.baidu.com/lssbing/blog/item/9a2dcb0f1183a1266059f38d.html

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.