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

Example Analysis of efficiency and Stability of file_get_contents and curl in php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "example analysis of the efficiency and stability of file_get_contents and curl in php", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and study the "example analysis of the efficiency and stability of file_get_contents and curl in php".

I have done a lot of products that crawl the content of other websites, and I am used to using the convenient and fast file_get_contents function, but I always encounter the problem of acquisition failure. Although the timeout is set according to the examples in the manual, it will not work most of the time:

The code is as follows:

$config ['context'] = stream_context_create (array (' http' = > array ('method' = > "GET")

'timeout' = > 5pm / this timeout is unstable and often does not work

)

))

At this point, take a look at the connection pool of the server and you will find a bunch of similar errors that give you a terrible headache:

File_get_contents (http://***): failed to open stream...

As a last resort, I installed the curl library and wrote a function replacement:

The code is as follows:

Function curl_file_get_contents ($durl) {

$ch = curl_init ()

Curl_setopt ($ch, CURLOPT_URL, $durl)

Curl_setopt ($ch, CURLOPT_TIMEOUT, 5)

Curl_setopt ($ch, CURLOPT_USERAGENT, _ USERAGENT_)

Curl_setopt ($ch, CURLOPT_REFERER,_REFERER_)

Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1)

$r = curl_exec ($ch)

Curl_close ($ch)

Return $r

}

In this way, there are no more problems except for the real network problems.

This is a test that others have done about curl and file_get_contents:

The number of seconds it takes for file_get_contents to grab google.com:

2.31319094

2.30374217

2.21512604

3.30553889

2.30124092

Time used by curl:

0.68719101

0.64675593

0.64326

0.81983113

0.63956594

There's a big gap, isn't it? Ha ha, from my experience, the two tools are not only different in speed, but also in stability. It is suggested that friends with high requirements for network data grabbing stability use the curl_file_get_contents function above, which is not only stable and fast, but also can impersonate the browser to deceive the target address.

The above is all the contents of the article "example Analysis of the efficiency and Stability of file_get_contents and curl in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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