Introduction to file_get_contents and curl_get_contents in php

Source: Internet
Author: User
Tags curl cpu usage high cpu usage

Share an actually used function:

The file_get_contents () function is the preferred method for reading the file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.

/* More stable than file_get_contents! $ Timeout is the timeout time in seconds. The default value is 1 s. */

The code is as follows: Copy code
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.111cn.net /');

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


Set it through default_socket_timeout in php. ini. The default timeout value is default_socket_timeout = 60.

The code is as follows: Copy code
Max_execution_time = 30
Default_socket_timeout = 60

Suppose you use file_get_contents to spend 45 while max_execution_time is 30. Will it time out?
The answer is NO, because max_execution_time does not affect the operating system call or stream operation.
Another point to note is that default_socket_timeout is calculated before the socket response. If a response is received, it will continue to be executed.

You can set it in the following three ways:

The code is as follows: Copy code

1 modify default_socket_timeout = 120 directly in php. ini
2 ini_set ('default _ socket_timeout ', 120 );
3 $ strm = stream_context_create (array (
'Http' => array (
'Timeout' = & gt; 120
        )
    )
);

Curl introduction

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

In addition, the optimization of file_get_contnets from the banquet can be viewed as follows:

First, run 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, 3 users, load average: 17.86, 11.16, 7.69
Task: 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: 8100996 k total, 4320108 k used, 3780888 k free, 772572 k buffers
Swap: 8193108 k total, 50776 k used, 8142332 k free, 412088 k cached

Pid user pr ni virt res shr s % CPU % mem time + COMMAND
10747 www 18 0 360 m 22 m 12 m R 100.6 0: 02. 60 php-cgi
10709 www 16 0 359 m 28 m 17 m R 96.8 0.4. 34 php-cgi
10745 www 18 0 360 m 24 m 14 m R 94.8 0.3. 51 php-cgi
10707 www 18 0 360 m 25 m 14 m S 77.4 0: 33. 48 php-cgi
10782 www 20 0 360 m 26 m 15 m R 75.5 0.3. 93 php-cgi
10708 www 25 0 360 m 22 m 12 m R 69.7 0.3. 16 php-cgi
10683 www 25 0 362 m 28 m 15 m R 54.2 0.4. 65 php-cgi
10711 www 25 0 360 m 25 m 15 m R 52.2 0.3. 25 php-cgi
10688 www 25 0 359 m 25 m 15 m R 38.7 0.3. 44 php-cgi
10719 www 25 0 360 m 26 m 16 m R 7.7 0.3. 59 php-cgi

Find the PID of one of the php-cgi processes with CPU 100% and run the following command to trace the PID:

Strace-p 10747

If the screen displays:

The code is as follows: Copy code

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)

The problem is caused by file_get_contents.

Set the timeout time to solve this problem. If curl is not installed, you must use this method.

The code is as follows: Copy code

$ Ctx = stream_context_create (array (
'Http' => array (
'Timeout' => 1 // set a timeout time, in seconds
       )  
   )  
);
File_get_contents ("http://www.111cn.net/", 0, $ ctx );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.