PHP uses CURL to crawl multiple web pages synchronously

Source: Internet
Author: User
General curl the way to grasp the Web page, is a page to grasp, suppose to catch 4 pages, the time taken is 5,10,7,5 seconds, the total sum of time is 5 + 10 + 7 + 5 = 27 seconds. If you can crawl multiple pages at the same time, the time spent 5,10,7,5 seconds, the total sum of time is 10 seconds. (The number of seconds to spend the most time)

In JavaScript you can use AJAX async (YAHOO.util.Connect.asyncRequest) to achieve this multi-threading effect in PHP with CURL.

Program (async.php)

The following are the referenced contents:
<?php
function Async_get_url ($url _array, $wait _usec = 0)
{
if (!is_array ($url _array))
return false;

$wait _usec = intval ($wait _usec);

$data = Array ();
$handle = Array ();
$running = 0;

$MH = Curl_multi_init (); Multi Curl Handler

$i = 0;
foreach ($url _array as $url) {
$ch = Curl_init ();

curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_returntransfer, 1); Return don ' t print
curl_setopt ($ch, Curlopt_timeout, 30);
curl_setopt ($ch, Curlopt_useragent, ' mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0);
curl_setopt ($ch, curlopt_followlocation, 1); 302 redirect
curl_setopt ($ch, Curlopt_maxredirs, 7);

Curl_multi_add_handle ($MH, $ch); Put curl resource into multi curl handler.

$handle [$i + +] = $ch;
}

/* Execute * *
do {
Curl_multi_exec ($MH, $running);

if ($wait _usec > 0)/* How long each connect will be
Usleep ($wait _usec); 250000 = 0.25 sec
while ($running > 0);

/* Read Data * *
foreach ($handle as $i => $ch) {
$content = Curl_multi_getcontent ($ch);
$data [$i] = (Curl_errno ($ch) = = 0)? $content: false;
}

/* Remove handle*/
foreach ($handle as $ch) {
Curl_multi_remove_handle ($MH, $ch);
}

Curl_multi_close ($MH);

return $data;
}
?>

Use

The following are the referenced contents:
<?php
$urls = Array (' http://example1.com ', ' http://example2.com ');
Print_r (Async_get_url ($urls)); [0] => example1, [1] => example2
?>

Test

sleep.php # Look at the effect of time extension

The following are the referenced contents:
<?php
Sleep (intval ($_get[' time '));
echo intval ($_get[' time '));
?>

The following are the referenced contents:
<?php
$url _array = Array (
' Http://example.com/sleep.php?time=5 ',
' Http://example.com/sleep.php?time=10 ',
' Http://example.com/sleep.php?time=7 ',
' Http://example.com/sleep.php?time=5 ',
);
Print_r (Async_get_url ($url _array));
The total time spent would be 10 seconds and printed [0] => 5, [1] =>, [2] => 7, [3] => 5
?>



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.