这篇文章主要为大家详细介绍了PHP利用file_get_contents与curl效率及稳定性的深入分析,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:
代码如下:
$config['context'] = stream_context_create(array(‘http' => array(‘method' => “GET”,
'timeout' => 5//这个超时时间不稳定,经常不奏效
)
));
代码如下:
<span style="color:#000000; font-weight:bold">function</span> curl_file_get_contents<span style="color:#009900">(</span><span style="color:#000088">$durl</span><span style="color:#009900">)</span><span style="color:#009900">{</span>
<span style="color:#000088">$ch</span> <span style="color:#339933">=</span> <span style="color:#990000">curl_init</span><span style="color:#009900">(</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_URL<span style="color:#339933">,</span> <span style="color:#000088">$durl</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_TIMEOUT<span style="color:#339933">,</span> <span style="color:#cc66cc">5</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_USERAGENT<span style="color:#339933">,</span> _USERAGENT_<span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_REFERER<span style="color:#339933">,</span>_REFERER_<span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_setopt</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#339933">,</span> CURLOPT_RETURNTRANSFER<span style="color:#339933">,</span> <span style="color:#cc66cc">1</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#000088">$r</span> <span style="color:#339933">=</span> <span style="color:#990000">curl_exec</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#990000">curl_close</span><span style="color:#009900">(</span><span style="color:#000088">$ch</span><span style="color:#009900">)</span><span style="color:#339933">;</span>
<span style="color:#b1b100">return</span> <span style="color:#000088">$r</span><span style="color:#339933">;</span>
<span style="color:#009900">}</span>
本文来自:http://www.q1010.com/173/15590-0.html
注:关于PHP利用file_get_contents与curl效率及稳定性的深入分析的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:file_get_contents
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。