Curl_multi series functions in PHP using examples _php tutorials

Source: Internet
Author: User

Examples of using Curl_multi series functions in PHP


Believe that many people on the PHP manual vague curl_multi A family of functions headache unceasingly, their documents are few, give the example is simple so that you can not learn, I have been looking for a lot of web pages, 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

In general, when thinking about using these functions, it is clear that the purpose should be to request multiple URLs at the same time, instead of one by one, it is better to loop to tune Curl_exec.

The steps are summarized as follows:

First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
It is important to note that the second parameter of Curl_multi_add_handle is a sub-handle from Curl_init.
Step three: Continue calling Curl_multi_exec
Fourth step: Loop call Curl_multi_getcontent as needed to get results
Fifth step: Call Curl_multi_remove_handle, and call curl_close for each word handle
Sixth step: Call Curl_multi_close

Here is an example of a simple online search, which the author calls the dirty, (I'll explain why Dirty later):

The 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.baidu.com/",
"http://www.google.com/",
"http://www.jb51.net/"
);
$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 process is almost like this, but this simple code has a fatal weakness, that is, in the Do loop, during the entire URL request is a dead loop, it can easily lead to CPU consumption of 100%.

Now let's improve it, there is a function curl_multi_select with almost no documentation, although C's Curl Library has a description of select, but the interface and usage in PHP are different from C.

Change the part of the above do to the following:

The code is 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 to wait for all the URL data to be completed before it becomes false, so here to use the Curl_multi_exec return value to determine whether there is data, when there is data when the call Curl_multi_exec, There is no data to enter the Select phase, and new data can be woken up and executed. The advantage here is that the CPU is useless.

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

Control the time-out for each request by curl_setopt before Curl_multi_add_handle:

The code is as follows:

curl_setopt ($ch, Curlopt_timeout, $timeout);

Determine if a time-out or other error was used before Curl_multi_getcontent: Curl_error ($conn [$i]);

http://www.bkjia.com/PHPjc/852812.html www.bkjia.com true http://www.bkjia.com/PHPjc/852812.html techarticle PHP Curl_multi Series function Use examples believe that many people in the PHP manual vague curl_multi A family of functions headache, their documents are few, give the example is simply let you have no ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.