Home Backend Development PHP Tutorial Analyze the application of curl_multi in php_PHP tutorial

Analyze the application of curl_multi in php_PHP tutorial

Jul 21, 2016 pm 03:00 PM
curl multi php right application manual of parse

I believe that many people have a headache with the curl_multi family of functions that are unclear in the PHP manual. They have few documents and the examples given are so simple that you cannot learn from them. I have also searched many web pages, but I have not found a complete application. example.
•curl_multi_add_handle
•curl_multi_close
•curl_multi_exec
•curl_multi_getcontent
•curl_multi_info_read
•curl_multi_init
•curl_multi_remove_handle
•curl_multi_select
Generally speaking, when you think of using When using these functions, the purpose should obviously be to request multiple URLs at the same time, rather than requesting them one by one. Otherwise, it is better to loop and adjust curl_exec by yourself.

The steps are summarized as follows:
Step 1: Call curl_multi_init
Step 2: Call curl_multi_add_handle in a loop
What needs to be noted in this step is that curl_multi_add_handle The second parameter is the subhandle from curl_init.
Step 3: Continue to call curl_multi_exec
Step 4: Call curl_multi_getcontent in a loop to obtain the results as needed
Step 5: Call curl_multi_remove_handle, and call curl_close for each word handle
Step 6: Call curl_multi_close

Here is a simple example found online, which the author calls a dirty example (I will explain why dirty later):

Copy codeThe code is as follows:

*
Here's a quick and dirty example for curl-multi from PHP, tested on PHP 5.0.0RC1 CLI / FreeBSD 5.2.1
*/

$connomains = array(
"http://www.cnn.com/",
"http://www.canada.com/",
"http://www. yahoo.com/"
);

$mh = curl_multi_init();

foreach ($connomains as $i => $url) {
$conn[$i]=curl_init($url);
curl_setopt($conn[$i],CURLOPT_RETURNTRANSFER,1);
curl_multi_add_handle ($mh,$conn[$i]);
}

do { $n=curl_multi_exec($mh,$active); } while ($active);

foreach ($connomains as $i => $url) {
$res[$i]=curl_multi_getcontent($conn[$i]);
curl_close($conn[$i]);
}

print_r($res);


The whole usage process is almost like this. However, this simple code has a fatal weakness, that is, in the do loop, it is dead during the entire url request. Loop, it can easily cause the CPU to occupy 100%.

Now let’s improve it. Here we need to use a function curl_multi_select that has almost no documentation. Although C’s curl library has instructions for select, the interface and usage in PHP are indeed different from those in C. .

Change the do paragraph above to the following:
Copy the code The code is as follows:

do {
                                                                                       ​while ($active and $mrc ​​== CURLM_OK) {
                                                                                                                                                                                                                                                                      $mrc ​​= curl_multi_exec($mh, $active);
                                                                                                                      🎜>                                                                                                                                                                                                                 
Because $active has to wait until all url data is received before it becomes false, so the return value of curl_multi_exec is used here to determine whether there is still data. When there is data, curl_multi_exec will be called continuously. If there is no data temporarily, it will enter the select stage. , it can be awakened to continue execution as soon as new data comes. The advantage here is that there is no unnecessary consumption of CPU.

In addition: There are some details that may sometimes be encountered:
Control the timeout of each request, do it through curl_setopt before curl_multi_add_handle:
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);

To determine whether there is a timeout or other errors, use: curl_error($conn[$i]);

before curl_multi_getcontent

Note: Use the multi_curl function of php with caution, because there are bugs in the combination of curl and php in some versions. So the code that you debugged without problems is likely to be incorrect on other machines.

For example, today I discovered that if the CURLOPT_USERAGENT attribute is set to certain values ​​in php5.2.2 with curl/7.16.2, the actual HTTP header sent will become a string of binary values.

It just so happened that the strip_tags function in this version of php did not handle binary data well, so I discovered this bug

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328037.htmlTechArticleI believe many people have headaches about the curl_multi family of functions that are unclear in the PHP manual. They have less documentation and more examples. It is simply that you have nothing to learn from. I have also found many web pages...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles