


Comparison of curl, fsocket, file_get_content functions in php
When I was working on a web thief program recently, I discovered that file_get_content could no longer meet the needs. I think that when reading remote content, file_get_content is not as good as curl except for being more convenient to use than curl To capture remote content, I have been using the file_get_content function before. In fact, I have known about the existence of such a good thing as curl, but after taking a look, I felt that it is quite complicated to use. It is not as simple as file_get_content, and the demand is not big. So I didn't learn to use curl. Some comparisons between curl and file_get_content in php Main differences: Curl supports many protocols, including FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. In other words, it can do many things that file_get_content cannot do. Curl can achieve remote acquisition and collection of content in PHP; implement FTP upload and download of PHP web version; implement simulated login; implement interface docking (API), data transmission; implement simulated cookies; download file breakpoint resume transfer, etc., the function is very powerful . After understanding some basic uses of curl, I found that it is not difficult. It is just a little difficult to remember some of the setting parameters, but we can just remember a few commonly used ones. Turn on curl: Because PHP does not support the curl function by default, if you want to use curl, you first need to enable this function in php.ini, that is, remove the semicolon in front of ;extension= php_curl.dll, then save and restart apache/iis . Basic syntax:
Recently, I need to obtain music data from other people's websites. I used the file_get_contents function, but I always encountered 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:
At this time, if I look at the server's connection pool, I will find a bunch of similar errors, which gives me a huge headache: file_get_contents(http://***): failed to open stream… Now I use the curl library instead and write a function replacement:
How to set up PHP file_get_contents timeout processing
$url='http://www.domain.com/';
Method 2: Open the url with fopen and get the content with get method
Method 3: Use the file_get_contents function to get the url in post mode
Method 4: Use the fsockopen function to open the url and get the complete data in get mode, including header and body
Method 5: Use the fsockopen function to open the url and get the complete data in POST mode , including header and body
Method 6: Use the curl library. Before using the curl library, you may need to check whether the curl extension has been turned on in php.ini
The three functions of curl, fsockopen and file_get_contents in php can all realize the collection of simulated speeches. What is the difference between the three, or is there anything to pay attention to? Zhao Yongbin: Sometimes when using file_get_contents() to call external files, it is easy to report an error due to timeout. Just change it to curl. The specific reason is not clear. The efficiency of curl is higher than file_get_contents() and fsockopen(). The reason is that CURL will automatically cache DNS information (the highlight is for me to test personally) Fan Jiapeng: file_get_contents curl fsockopen Selective operations in the current requested environment, without generalization: Let’s look at the KBI application developed by our company: Just started using: file_get_contents Later adopted: fsockopen Last used till now: curl (Remotely) My personal understanding is as follows (please point out if it is wrong, please add if it is not correct) file_get_contents needs to enable allow_url_fopen in php.ini. When requesting http, http_fopen_wrapper is used. It is ok if you don't know keeplive.curl. file_get_contents() has high single execution efficiency and returns information without header. This is no problem when reading ordinary files, but problems will occur when reading remote files. If you want to make a continuous connection, request multiple pages multiple times. Then there will be problems with file_get_contents and fopen. The obtained content may also be incorrect. So when doing some similar collection work, there will definitely be a problem. Sock is relatively low-level, troublesome to configure, and difficult to operate. Return complete information. Pan Shaoning-Tencent: Although file_get_contents can get the content of a certain URL, it cannot post get it. Curl can post and get. Head information can also be obtained Sockets are lower level. Can be set to interact based on UDP or TCP protocol If file_get_contents and curl can do it, socket can do it. What socket can do, curl may not be able to do. file_get_contents more often just pulls data. It is more efficient and simpler. I have also encountered Zhao's situation. I set the host through CURL and it was OK. This has something to do with the network environment. That’s it. The above introduces the similarities and differences in the usage of url, fsocket, and file_get_content functions in PHP through examples. I hope it will be helpful to everyone. |

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

Alipay PHP...

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,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
