Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PHP built-in access to resources timeout time_out file_get_contents read_file how to use

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the PHP built-in access to resources timeout time_out file_get_contents read_file how to use, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

Ask a question

I cycle through file_get_contents to grab a bunch of url, but always stop at less than the 100th URL and prompt me: "Warning: file_get_contents (URL) [function.file-get-"

Contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Read timed out

In D:\ website\ extra.php on line 65 "

I already have set_time_limit (0) at the beginning of the program, so what could be the reason for the above error?

Answer

Set_time_limit just sets the timeout for your PHP program, not the timeout for the file_get_contents function to read URL.

Judging from the warning message, there is a server 500 error in the crawled web page, and his program may have timed out.

If you want to change the file_get_contents timeout, you can use the timeout parameter of resource $context:

The code is as follows:

$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)

In this way, the timeout of the readfile function is set to 10 seconds. If you are careful enough, you will find that there are some other configurations in the array. The http in the first dimension is the specified network protocol, and the method in 2D is the request method get,post,head of http. Timeout is the timeout. I think a lot of people will use php's built-in file_get_contents function to download web pages because it's easy to use. Many people also use it simply by passing a link and automatically sending a get request and downloading the content of the page. If there are more complex situations, such as using POST requests, using proxy downloads, defining User-Agent, and so on, many people will think that this function cannot do such a thing, so they will choose other ways, such as curl, to implement it. In fact, file_get_contents can also do these things.

It sets the context of the http request through its third parameter.

For the supported settings and usage, please see the official description: http://www.php.net/manual/en/context.http.php

Attachment: at present, I know that the php built-in functions that support context parameters are file_get_contents,file_put_contents,readfile,file,fopen,copy (it is estimated that all such functions support it, to be confirmed).

The copy code is as follows:

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.yifu.info', $data)

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

Thank you for reading this article carefully. I hope the article "how to use the timeout time_out file_get_contents read_file of PHP built-in access resources" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report