Home >php教程 >php手册 >php中file_get_contents和curl_get_contents介绍

php中file_get_contents和curl_get_contents介绍

WBOY
WBOYOriginal
2016-06-13 10:01:39836browse

php中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。

分享一个实际在用的函数:

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

/*比file_get_contents稳定的多!$timeout为超时时间,单位是秒,默认为1s。*/

 代码如下 复制代码
function curl_get_contents($url,$timeout=1) {
  $curlHandle = curl_init();
  curl_setopt( $curlHandle , CURLOPT_URL, $url );
  curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
  curl_setopt( $curlHandle , CURLOPT_TIMEOUT, $timeout );
  $result = curl_exec( $curlHandle );
  curl_close( $curlHandle );
  return $result;
}
$hx = curl_get_contents('http://www.bKjia.c0m/');

相信使用过file_get_contents函数的朋友都知道,当获取的$url访问不了时,会导致页面漫长的等待,甚至还能导致PHP进程占用CPU达100%,因此这个函数就诞生了。


通过php.ini中的default_socket_timeout设置,默认超时时间是default_socket_timeout = 60

 代码如下 复制代码
max_execution_time = 30
default_socket_timeout = 60

假设你使用file_get_contents花费45,而max_execution_time是30,它将超时吗?
答案是NO,因为max_execution_time不影响操作系统调用或stream操作
另一点要指出的的是default_socket_timeout是在socket响应之前计算的,只要得到响应,将会一直执行下去

可以通过以下三种方式设置

 代码如下 复制代码

1 直接在php.ini中修改  default_socket_timeout =120
2 ini_set('default_socket_timeout',    120);  
3 $strm = stream_context_create(array(
    'http' => array(
        'timeout' => 120
        )
    )
);

curl的一些常识介绍

保留原file_get_contents函数的原因是当读取本地文件时,用原生的file_get_contents显然更合适。

另来自张宴的file_get_contnets的优化,具体可看:

首先,使用 top 命令查看 CPU 使用率较高的 php-cgi 进程。

 

 代码如下 复制代码

top - 10:34:18 up 724 days, 21:01,  3 users,  load average: 17.86, 11.16, 7.69
Tasks: 561 total,  15 running, 546 sleeping,   0 stopped,   0 zombie
Cpu(s):  5.9%us,  4.2%sy,  0.0%ni, 89.4%id,  0.2%wa,  0.0%hi,  0.2%si,  0.0%st
Mem:   8100996k total,  4320108k used,  3780888k free,   772572k buffers
Swap:  8193108k total,    50776k used,  8142332k free,   412088k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                              
10747 www       18   0  360m  22m  12m R 100.6 0.3    0:02.60 php-cgi                                                                                                             
10709 www       16   0  359m  28m  17m R 96.8  0.4    0:11.34 php-cgi                                                                                                              
10745 www       18   0  360m  24m  14m R 94.8  0.3    0:39.51 php-cgi                                                                                                              
10707 www       18   0  360m  25m  14m S 77.4  0.3    0:33.48 php-cgi                                                                                                              
10782 www       20   0  360m  26m  15m R 75.5  0.3    0:10.93 php-cgi                                                                                                              
10708 www       25   0  360m  22m  12m R 69.7  0.3    0:45.16 php-cgi                                                                                                              
10683 www       25   0  362m  28m  15m R 54.2  0.4    0:32.65 php-cgi                                                                                                              
10711 www       25   0  360m  25m  15m R 52.2  0.3    0:44.25 php-cgi                                                                                                              
10688 www       25   0  359m  25m  15m R 38.7  0.3    0:10.44 php-cgi                                                                                                              
10719 www       25   0  360m  26m  16m R  7.7  0.3    0:40.59 php-cgi

  找其中一个 CPU 100% 的 php-cgi 进程的 PID,用以下命令跟踪一下:

strace -p 10747

  如果屏幕显示:

 代码如下 复制代码

select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)
select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})
poll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)

那么,就可以确定是 file_get_contents() 导致的问题了。

同样是设置超时时间来解决这个问题。如果没装curl,就必须得用这个方式了。

 代码如下 复制代码

$ctx = stream_context_create(array(  
   'http' => array(  
       'timeout' => 1 //设置一个超时时间,单位为秒  
       )  
   )  
);  
file_get_contents("http://www.hzhuti.com/", 0, $ctx);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn