PHP using curl alternative file_get_contents

Php beginner friends, it is easy to turn a mistake, in writing or by calling api collection procedures wired interfaces will always take into account the content using file_get_contents function to perhaps little traffic program is definitely no impact, but views that enhance the very tragedy, you will find that the server load soared, finally entering the company server downtime will encounter this problem, then use this command to replace the curl, and disable remote file_get_contents, it supports many protocols:. FTP, FTPS, HTTP, HTTPS, GOPHER , TELNET, DICT, FILE and LDAP.
<?php
$url='http://www.baidu.com';

$ch = curl_init();

$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

echo $file_contents;

//function_exists()替换

if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
?>
Disable file_get_contents: Open php.ini, configured as follows:
allow_url_fopen = On
改为
allow_url_fopen = Off
If fastcgi restart php-fpm, if it is directly apache + php to restart apache. File_get_contents and curl comparison results. [Caption ID = "attachment_1827" align = left = "AlignCenter" width = "626"] curl alternative file_get_contents curl Alternatively file_get_contents [/ caption ] Fuzhou file_get_contents: Fuzhou, Fujian 0.4679060000 seconds curl: 0.3150200000 seconds here are using file_get_contents and curl use Taobao ip library to obtain IP address information, you can find file_get_contents and curl performance gap is quite big .file_get_contents consuming 0.467 second, curl using 0.315 seconds 0.152 seconds worse, 30% of the performance gap. Taobao IP library to use to open the connection please indicate the source: http: //www.ttlsa.com/html/1821.html

Reproduced in: https: //my.oschina.net/766/blog/210992

Guess you like

Origin blog.csdn.net/weixin_33858485/article/details/91493091