Examples of Curl_multi series functions in PHP _php examples

Source: Internet
Author: User
Tags curl

I believe many people in the PHP Manual of the vague curl_multi a family of functions headaches, they have fewer documents, give examples is simple so you can not learn from, I have been looking for a lot of web pages, have not seen a complete application examples.

    • 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 you think about using these functions, you should obviously want to request multiple URLs at the same time, rather than a single request, or you might as well cycle to tune curl_exec.

The steps are summarized as follows:

First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
In this step, it should be noted that the second parameter of Curl_multi_add_handle is a curl_init handle.
Step three: Keep calling Curl_multi_exec
Step Fourth: Loop call curl_multi_getcontent to get results as needed
Step Fifth: Call Curl_multi_remove_handle and call curl_close for each word handle
Step Sixth: Call Curl_multi_close

Here is a simple example of a search on the web, the author of which is called the example of dirty (I will explain why dirty later):

Copy Code code as follows:

/*
Here's a quick and dirty example for Curl-multi to 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);

This is pretty much what the whole use process is, but the simple code has a fatal weakness, that is, in the part of Do loop, it is a dead loop throughout the URL request, and it can easily cause the CPU to occupy 100%.

Now we're going to improve it, and here we'll use a function with almost no documentation curl_multi_select, although C's Curl Library has a description of the Select, but the interface and usage in PHP are different from C.

Change the paragraph above to do the following:

Copy Code code 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 become false, so here used the return value of curl_multi_exec to judge whether there is data, when there is data, call Curl_multi_exec, There is no data at the moment, and the new data can be awakened to continue execution as soon as it enters the select phase. The advantage here is that the unnecessary consumption of the CPU is gone.

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

Control the timeout for each request by curl_setopt before Curl_multi_add_handle:

Copy Code code as follows:
curl_setopt ($ch, Curlopt_timeout, $timeout);

Determine if the timeout or other errors are used before Curl_multi_getcontent: Curl_error ($conn [$i]);

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.