php curl sends http requests in batches
Introduction: In Android 4.0 development, sending Http requests is no longer allowed to be executed in the main process and must be executed in a thread. The reason is that the response time of the Http interface may block the event monitoring of the main process (the same is true for .Net development). However, since PHP does not have the concept of multi-threading, how to efficiently execute multiple http requests in PHP? The answer is to use curl_multi_init, so I did an experiment.
The following is the http interface that simulates the request. The code is very simple. The sleep time is controlled by the parameter time passed in by get.
$s_time=intval($_GET['time']); sleep($s_time); echo 'hello';
Next, just use curl_init. The code is as follows:
$start=microtime(true); for($i=1;$i<=5;++$i) { $ch=curl_init("http://test.binbin.com/curl/test.php?time={$i}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); } $end=microtime(true); echo $end-$start;
The execution time is about 15 seconds, which is the sum of all sleep times. Now, let’s take a look at the time using curl_multi_init
$start=microtime(true); $ch_list=array(); $multi_ch=curl_multi_init(); for($i=1;$i<=5;++$i) { $ch_list[$i]=curl_init("http://test.binbin.com/curl/test.php?time={$i}"); curl_setopt($ch_list[$i], CURLOPT_RETURNTRANSFER, true); curl_multi_add_handle($multi_ch, $ch_list[$i]); } $running=false; do { usleep(10000); curl_multi_exec($multi_ch, $running); }while ($running>0); $end=microtime(true); echo $end-$start;
Postscript: I saw many people reporting on blogs the problem of excessive CPU usage using curl_multi_init. In fact, it can be solved by adding usleep. Because if the data is not returned, curl_multi_exec will continue to execute, consuming CPU resources.
The above introduces php curl to send http requests in batches, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

HTTP status code 520 means that the server encountered an unknown error while processing the request and cannot provide more specific information. Used to indicate that an unknown error occurred when the server was processing the request, which may be caused by server configuration problems, network problems, or other unknown reasons. This is usually caused by server configuration issues, network issues, server overload, or coding errors. If you encounter a status code 520 error, it is best to contact the website administrator or technical support team for more information and assistance.

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

HTTP status code 403 means that the server rejected the client's request. The solution to http status code 403 is: 1. Check the authentication credentials. If the server requires authentication, ensure that the correct credentials are provided; 2. Check the IP address restrictions. If the server has restricted the IP address, ensure that the client's IP address is restricted. Whitelisted or not blacklisted; 3. Check the file permission settings. If the 403 status code is related to the permission settings of the file or directory, ensure that the client has sufficient permissions to access these files or directories, etc.

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

Understand the meaning of HTTP 301 status code: common application scenarios of web page redirection. With the rapid development of the Internet, people's requirements for web page interaction are becoming higher and higher. In the field of web design, web page redirection is a common and important technology, implemented through the HTTP 301 status code. This article will explore the meaning of HTTP 301 status code and common application scenarios in web page redirection. HTTP301 status code refers to permanent redirect (PermanentRedirect). When the server receives the client's

How to use NginxProxyManager to implement automatic jump from HTTP to HTTPS. With the development of the Internet, more and more websites are beginning to use the HTTPS protocol to encrypt data transmission to improve data security and user privacy protection. Since the HTTPS protocol requires the support of an SSL certificate, certain technical support is required when deploying the HTTPS protocol. Nginx is a powerful and commonly used HTTP server and reverse proxy server, and NginxProxy

HTTP Status Code 200: Explore the Meaning and Purpose of Successful Responses HTTP status codes are numeric codes used to indicate the status of a server's response. Among them, status code 200 indicates that the request has been successfully processed by the server. This article will explore the specific meaning and use of HTTP status code 200. First, let us understand the classification of HTTP status codes. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. Among them, 2xx indicates a successful response. And 200 is the most common status code in 2xx
