


Comparative analysis of file_get_contents and curl performance in php
This article mainly introduces the performance comparison between file_get_contents and curl in PHP, and analyzes the difference between file_get_contents and curl in detail as well as the comparison of operating efficiency in the form of examples. Friends in need can refer to it
The examples in this article describe Comparative analysis of file_get_contents and curl performance in PHP. Share it with everyone for your reference. The details are as follows:
If you don’t analyze the performance carefully in PHP, you will find that file_get_contents and curl have a lot in common. They can both collect files and open files, but if you compare them carefully, you will find many differences. , let’s take a look at the difference between file_get_contents and curl.
The difference between fopen, file_get_contents, and curl functions in PHP:
1. fopen /file_get_contents will re-do the DNS query for each request and does not cache the DNS information. But CURL will automatically cache DNS information. Requests for web pages or images under the same domain name only require one DNS query. This greatly reduces the number of DNS queries. So the performance of CURL is much better than fopen /file_get_contents.
2.fopen /file_get_contents When requesting HTTP, http_fopen_wrapper is used and will not keeplive. But curl can. In this way, curl will be more efficient when requesting multiple links multiple times.
3.fopen / file_get_contents function will be affected by the allow_url_open option configuration in the php.ini file. If the configuration is turned off, this function will be disabled. Curl is not affected by this configuration.
4.curl can simulate a variety of requests, such as POST data, form submission, etc. Users can customize requests according to their own needs. And fopen/file_get_contents can only use the get method to obtain data.
file_get_contents When obtaining remote files, the results will be stored in a string, while the fiels function will store them in array form
Therefore, I still prefer to use curl to access the remote URL. Php has curl module extension, which is very powerful.
After talking for a long time, everyone may say that there is no comparison in performance, so let’s take a look
Recently we need to obtain music data from other people’s websites. I have used the file_get_contents function, but I always encounter the problem of failure to obtain it. Although I set the timeout according to the examples in the manual, it does not work most of the time:
Copy code The code is as follows:
$config['context'] = stream_context_create(array('http' => array('method' => "GET", 'timeout' => 5//这个超时时间不稳定,经常不奏效 ) ));
At this time, if I look at the server's connection pool, I will find a bunch of similar errors, which gives me a headache:
file_get_contents(http://***): failed to open stream...
Now I use the curl library and write a function replacement:
Copy code The code is as follows:
function curl_file_get_contents($durl){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $durl); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); curl_setopt($ch, CURLOPT_REFERER,_REFERER_); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $r = curl_exec($ch); curl_close($ch); return $r; }
Like this, No more issues other than real network issues.
This is a test done by others about curl and file_get_contents:
The number of seconds it takes for file_get_contents to crawl google.com:
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl usage time:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
Is there a big difference? Haha, from my experience, these two tools are not only different in speed, but also in stability.
It is recommended that friends who have high requirements for the stability of network data capture use the curl_file_get_contents function above. Not only is it stable and fast, but it can also fake the browser and deceive the target address.
Look at another example
The comparison results of curl and file_get_contents are posted later. In addition to the performance comparison of curl and file_get_contents, it also includes their performance comparison. Before talking about it, take a look at the following result chart:
The performance comparison between curl and file_get_contents PHP source code is as follows:
Copy code The code is as follows:
<?php /** * 通过淘宝IP接口获取IP地理位置 * @param string $ip * @return: string **/ function getCityCurl($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $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); $ipinfo=json_decode($file_contents); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ipinfo=json_decode(file_get_contents($url)); if($ipinfo->code=='1'){ return false; } $city = $ipinfo->data->region.$ipinfo->data->city; return $city; } // for file_get_contents $startTime=explode(' ',microtime()); $startTime=$startTime[0] + $startTime[1]; for($i=1;$i<=10;$i++) { echo getCity("121.207.247.202")."</br>"; } $endTime = explode(' ',microtime()); $endTime = $endTime[0] + $endTime[1]; $totalTime = $endTime - $startTime; echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>"; //for curl $startTime2=explode(' ',microtime()); $startTime2=$startTime2[0] + $startTime2[1]; for($i=1;$i<=10;$i++) { echo getCityCurl('121.207.247.202')."</br>"; } $endTime2 = explode(' ',microtime()); $endTime2=$endTime2[0] + $endTime2[1]; $totalTime2 = $endTime2 - $startTime2; echo "curl:".number_format($totalTime2, 10, '.', "")." seconds"; ?>
Test access
file_get_contents speed: 4.2404510975 seconds
curl speed: 2.8205530643 seconds
curl is about 30% faster than file_get_contents. The most important thing is that the server load is lower.
Summary
When file_get_contents processing is frequent and small, it feels good to use it. Nothing unusual. If your file is processed by 1k people. Then your server CPU is waiting for a boost. Therefore, I recommend that you and everyone use the curl library when writing PHP code in the future.
Related recommendations:
thinkphp3.2 realizes the method of calling other modules across controllers
# #
The above is the detailed content of Comparative analysis of file_get_contents and curl performance in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
