Home >Backend Development >PHP Tutorial >Introduction to file_get_contents and curl_get_contents in php_PHP tutorial

Introduction to file_get_contents and curl_get_contents in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:331288browse

Introduction to file_get_contents and curl_get_contents in php. Friends in need can refer to it.

Share an actually used function:

The file_get_contents() function is the preferred method for reading the contents of a file into a string. If supported by the operating system, memory mapping technology is also used to enhance performance.

/*Much more stable than file_get_contents! $timeout is the timeout time, the unit is seconds, and the default is 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/');

I believe friends who have used the file_get_contents function know that when the obtained $url cannot be accessed, it will cause the page to wait for a long time, and even cause the PHP process to occupy 100% of the CPU, so this function was born.


Through the default_socket_timeout setting in php.ini, the default timeout is default_socket_timeout = 60

The code is as follows Copy code
max_execution_time = 30
 代码如下 复制代码
max_execution_time = 30
default_socket_timeout = 60
default_socket_timeout = 60


Suppose you use file_get_contents which takes 45 and max_execution_time is 30, will it time out?
The answer is NO, because max_execution_time does not affect operating system calls or stream operations Another point to point out is that default_socket_timeout is calculated before the socket responds. As long as a response is received, it will continue to be executed

Can be set in the following three ways
 代码如下 复制代码

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

The code is as follows Copy code
1 Modify directly in php.ini default_socket_timeout =120 2 ini_set('default_socket_timeout', 120); 3 $strm = stream_context_create(array( 'http' => array(          'timeout' => 120 ) ) );

Some common sense introduction to curl

The reason for retaining the original file_get_contents function is that when reading local files, it is obviously more appropriate to use the native file_get_contents.

Another optimization of file_get_contnets from Zhang Yan, please see:

First, use the top command to view the php-cgi process with high CPU usage.

The code is as follows Copy code

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)

复制代码

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)

 代码如下 复制代码

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

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

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

 代码如下 复制代码
$ctx = stream_context_create(array(  
   'http' => array(  
       'timeout' => 1 //设置一个超时时间,单位为秒  
       )  
   )  
);  
file_get_contents("http://www.hzhuti.com/", 0, $ctx); http://www.bkjia.com/PHPjc/631590.html
www.bkjia.comtruehttp://www.bkjia.com/PHPjc/631590.htmlTechArticlephp中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。 分享一个实际在用的函数: file_get_contents() 函数是用于将文件的内容读入...
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