Examples of using curl_multi series functions in php

  • 2021-07-10 18:59:18
  • OfStack

I believe many people have a headache for the vague functions of curl_multi1 family in php manual. They have few documents, and the examples given are simple for you to learn from. I have also looked for many web pages, but I have not seen 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 thinking of using these functions, the purpose should obviously be to request multiple url at the same time, instead of requesting one by one in turn, otherwise it is better to call curl_exec in a loop.

The steps are summarized as follows:

Step 1: Call curl_multi_init
Step 2: Loop to call curl_multi_add_handle
Note in this step that the second parameter of curl_multi_add_handle is the child handle from curl_init.
Step 3: Keep calling curl_multi_exec
Step 4: Loop to call curl_multi_getcontent as needed to get the result
Step 5: Call curl_multi_remove_handle and call curl_close for each word handle
Step 6: Call curl_multi_close

Here's a simple example of what the authors call dirty (I'll explain why dirty later):


/*
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.baidu.com/",
"http://www.google.com/",
"https://www.ofstack.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);

This is pretty much the case, but the Achilles heel of this simple code is that during the do loop, which is a dead loop during the entire url request, it can easily cause CPU to occupy 100%.

Now let's improve it. Here we need a function curl_multi_select with almost no documentation. Although curl library of C explains select, the interface and usage in php are different from those in C.

Change the paragraph above do to read as follows:


           do {
                        $mrc = curl_multi_exec($mh,$active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
                while ($active and $mrc == CURLM_OK) {
                        if (curl_multi_select($mh) != -1) {
                                do {
                                        $mrc = curl_multi_exec($mh, $active);
                                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
                        }
                }

Because $active will not become false until all url data are accepted, the return value of curl_multi_exec is used here to judge whether there is still data. When there is data, curl_multi_exec will be called continuously, and if there is no data for the time being, it will enter select stage, and new data 1 can be awakened to continue execution. The advantage here is that the unnecessary consumption of CPU is gone.

In addition: There are 1 details that may sometimes be encountered:

Control the timeout for every 1 request by curl_setopt before curl_multi_add_handle:

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

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


Related articles: