Search Posts

File_get_contents increases the time limit for timeouts

When using the file_get_contents function, there will often be a timeout situation. Here you should check the error prompt to see what kind of error, the more common is the read timeout. In this case, you can try to avoid it by some means. Or solve it. Here are two simple introductions:

First, increase the time limit of timeout

Note here that set_time_limit simply sets the timeout for your PHP program, not the timeout for the file_get_contents function to read the URL.
I initially thought that set_time_limit also affected file_get_contents, which was tested and was invalid. The real modification of the file_get_contents delay can be done with the timeout parameter of resource $context:




Second, if there is a delay, then try it a few times.

Sometimes the failure is caused by factors such as the network. There is no solution, but you can modify the program. If you fail, try again. If you still fail, give up, because file_get_contents() will return FALSE if it fails, so you can write code like this:




The above method has been corrected for the timeout.

There is a potential error here: if the file does not exist, the program will enter an infinite loop, so the number of judgments is added. Suppose only check 4 times.
Here is the code:




After 4 times, I haven’t got it yet. Go directly to the next function! The above just terminates all operations, the functions here can be configured by themselves

So what about Post? Attentive point someone found ‘method’ => “GET”, right! Can it be set to post? Baidu found the relevant information, really can! And someone wrote the postal value function of the cottage version, as follows:




OK, the above function is perfect, which not only solves the timeout control but also solves the Post value. Combined with Kangsheng’s improved version of the RC4 encryption and decryption algorithm, it is much simpler to make a highly secure webservice.

Leave a Reply