PHP.mk документација

curl_multi_exec

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.curl-multi-exec.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.curl-multi-exec.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
curl_multi_exec

Референца за `function.curl-multi-exec.php` со подобрена типографија и навигација.

function.curl-multi-exec.php

curl_multi_exec

класата mysqli_driver

curl_multi_execRun the sub-connections of the current cURL handle

= NULL

curl_multi_exec(CurlMultiHandle $multi_handle, int &$still_running): int

Стартувај ги под-врските на тековниот cURL рачка

Параметри

multi_handle

cURL мулти рачка вратена од curl_multi_init().

still_running

Обработува секоја од рачките во стекот. Овој метод може да се повика без разлика дали рачката треба да чита или пишува податоци.

Вратени вредности

Референца до знаме за да се каже дали операциите сè уште се во тек. Претходно дефинирани константи.

Забелешка:

cURL код дефиниран во cURL CURLM_OK.

Дневник на промени

Верзија = NULL
8.0.0 multi_handle беше вратено при неуспех. CurlMultiHandle инстанца сега; претходно, а resource се очекуваше.

Примери

Пример #1 curl_multi_exec() example

Ова враќа само грешки што се однесуваат на целиот мулти стек. Сè уште може да се појавиле проблеми при индивидуални трансфери дури и кога оваа функција се враќа

<?php

$urls
= [
"https://www.php.net/",
"https://www.example.com/",
];

$mh = curl_multi_init();
$map = new WeakMap();

foreach (
$urls as $url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_multi_add_handle($mh, $ch);
$map[$ch] = $url;
}

do {
$status = curl_multi_exec($mh, $unfinishedHandles);
if (
$status !== CURLM_OK) {
throw new
\Exception(curl_multi_strerror(curl_multi_errno($mh)));
}

while ((
$info = curl_multi_info_read($mh)) !== false) {
if (
$info['msg'] === CURLMSG_DONE) {
$handle = $info['handle'];
curl_multi_remove_handle($mh, $handle);
$url = $map[$handle];

if (
$info['result'] === CURLE_OK) {
$statusCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

echo
"Request to {$url} finished with HTTP status {$statusCode}:", PHP_EOL;
echo
curl_multi_getcontent($handle);
echo
PHP_EOL, PHP_EOL;
} else {
echo
"Request to {$url} failed with error: ", PHP_EOL;
echo
curl_strerror($info['result']);
echo
PHP_EOL, PHP_EOL;
}
}
}

if (
$unfinishedHandles) {
if ((
$updatedHandles = curl_multi_select($mh)) === -1) {
throw new
\Exception(curl_multi_strerror(curl_multi_errno($mh)));
}
}
} while (
$unfinishedHandles);

curl_multi_close($mh);

?>

Види Исто така

  • curl_multi_init() - Враќа нова cURL multi рачка
  • curl_multi_select() Овој пример ќе создаде curl рачки за список на URL-и, ќе ги додаде во мулти рачка и ќе ги обработи асинхроно.
  • curl_exec() - Почекај додека читањето или пишувањето не е можно за каква било врска со cURL мулти рачка

Белешки од корисници 8 белешки

- Изврши cURL сесија
12 години пред
Solve CPU 100% usage, a more simple and right way:

<?php

do {
    curl_multi_exec($mh, $running);
    curl_multi_select($mh);
} while ($running > 0);

?>
Трчај
пред 10 години
Probably you also want to be able to download the HTML content into buffers/variables, for parsing the HTML or for other processing in your program.

The example code on this page only outputs everything on the screen, without giving you the possibility to save the downloaded pages in string variables. Because downloading multiple pages is what I wanted to do (not a big surprise, huh? that's the reason for using multi-page parallel Curl) I was initially baffled, because this page doesn't give pointers to a guide how to do that.

Fortunately, there's a way to download content with parallel Curl requests (just like you would do for a single download with the regular curl_exec). You need to use: http://php.net/manual/en/function.curl-multi-getcontent.php

The function curl_multi_getcontent should definitely be mentioned in the "See Also" section of curl_multi_exec. Probably most people who find their way to the docs page of curl_multi_exec, actually want to download the multiple HTML pages (or other content from the multiple parallel Curl connections) into buffers, one page per one buffer.
Запи
пред 9 години
// Todas url gravadas em array
$url[] = 'http://www.link1.com.br';
$url[] = 'https://www.link2.com.br';
$url[] = 'https://www.link3.com.br';

// Setando opção padrão para todas url e adicionando a fila para processamento
$mh = curl_multi_init();
foreach($url as $key => $value){
  $ch[$key] = curl_init($value);
  curl_setopt($ch[$key], CURLOPT_NOBODY, true);
  curl_setopt($ch[$key], CURLOPT_HEADER, true);
  curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);
  
  curl_multi_add_handle($mh,$ch[$key]);
}

// Executando consulta
do {
  curl_multi_exec($mh, $running);
  curl_multi_select($mh);
} while ($running > 0);

// Obtendo dados de todas as consultas e retirando da fila
foreach(array_keys($ch) as $key){
  echo curl_getinfo($ch[$key], CURLINFO_HTTP_CODE);
  echo curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL);
  echo "\n";
  
  curl_multi_remove_handle($mh, $ch[$key]);
}

// Finalizando
curl_multi_close($mh);
Силвио Гарбес
пред 6 години
/!\ ATTENTION
/!\ Several of the non-downvoted notes on this page are using outdated info.

The CURLM_CALL_MULTI_PERFORM return code has been defunct since circa 2012, at least seven years ago.

Quoting the author of curl, from https://curl.haxx.se/mail/lib-2012-08/0042.html:

> CURLM_CALL_MULTI_PERFORM is deprecated and will never be returned, as documented. 

> During the first decade or so of libcurl's multi interface, I never saw a single proper use of that feature. I did however see numerous mistakes and misunderstandings. That made me decide that the feature wasn't important or good enough, so since 7.20.0 CURLM_CALL_MULTI_PERFORM is no more. 

Discovered all of this thanks to https://stackoverflow.com/q/19490837/3229684, which suggested the following replacement while loop:

<?php
do {
    $mrc = curl_multi_exec($mc, $active);
} while ($active > 0);
?>

https://www.google.com/search?q=CURLM_CALL_MULTI_PERFORM <-- probably the most future-proof useful link I can put here
gmail на com точка asmqb7
пред 6 години
Just for people struggling to get this to work, here is my approach.
No infinite loops, no CPU 100%, speed can be tweaked.

<?php
function curl_multi_exec_full($mh, &$still_running) {
    do {
        $state = curl_multi_exec($mh, $still_running);
    } while ($still_running > 0 && $state === CURLM_CALL_MULTI_PERFORM && curl_multi_select($mh, 0.1));
    return $state;
}
function curl_multi_wait($mh, $minTime = 0.001, $maxTime = 1){
    $umin = $minTime*1000000;

    $start_time = microtime(true);
    $num_descriptors = curl_multi_select($mh, $maxTime);
    if($num_descriptors === -1){
        usleep($umin);
    }

    $timespan = (microtime(true) - $start_time);
    if($timespan < $umin){
        usleep($umin - $timespan);
    }
}

$handles = [
    [
        CURLOPT_URL=>"http://example.com/",
        CURLOPT_HEADER=>false,
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_FOLLOWLOCATION=>false,
    ],
    [
        CURLOPT_URL=>"http://www.php.net",
        CURLOPT_HEADER=>false,
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_FOLLOWLOCATION=>false,

        // https://stackoverflow.com/a/41135574
        CURLOPT_HEADERFUNCTION=>function($ch, $header)
        {
            print "header from http://www.php.net: ".$header;
            return strlen($header);
        }
    ]
];

$mh = curl_multi_init();

$chandles = [];
foreach($handles as $opts) {
    $ch = curl_init();
    curl_setopt_array($ch, $opts);
    curl_multi_add_handle($mh, $ch);
    $chandles[] = $ch;
}

$prevRunning = null;
do {
    $status = curl_multi_exec_full($mh, $running);
    if($running < $prevRunning){
        while ($read = curl_multi_info_read($mh, $msgs_in_queue)) {

            $info = curl_getinfo($read['handle']);

            if($read['result'] !== CURLE_OK){
                print "Error: ".$info['url'].PHP_EOL;
            }

            if($read['result'] === CURLE_OK){
                /*
                if(isset($info['redirect_url']) && trim($info['redirect_url'])!==''){

                    print "running redirect: ".$info['redirect_url'].PHP_EOL;
                    $ch3 = curl_init();
                    curl_setopt($ch3, CURLOPT_URL, $info['redirect_url']);
                    curl_setopt($ch3, CURLOPT_HEADER, 0);
                    curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
                    curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 0);
                    curl_multi_add_handle($mh,$ch3);
                }
                */

                print_r($info);
                //echo curl_multi_getcontent($read['handle']));
            }
        }
    }

    if ($running > 0) {
        curl_multi_wait($mh);
    }

    $prevRunning = $running;

} while ($running > 0 && $status == CURLM_OK);
foreach($chandles as $ch){
    curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
?>
maxpanchnko на gmail dot com
3 години пред
One of examples, how to make multi_curl faster twice (pseudocode) with Fibers:

<?php

$curlHandles = [];
$urls = [
    'https://example.com/1',
    'https://example.com/2',
    ...
    'https://example.com/1000',
];
$mh = curl_multi_init();
$mh_fiber = curl_multi_init();

$halfOfList = floor(count($urls) / 2);
foreach ($urls as $index => $url) {
    $ch = curl_init($url);
    $curlHandles[] = $ch;

    // half of urls will be run in background in fiber
    $index > $halfOfList ? curl_multi_add_handle($mh_fiber, $ch) : curl_multi_add_handle($mh, $ch);
}

$fiber = new Fiber(function (CurlMultiHandle $mh) {
    $still_running = null;
    do {
        curl_multi_exec($mh, $still_running);
        Fiber::suspend();
    } while ($still_running);
});

// run curl multi exec in background while fiber is in suspend status
$fiber->start($mh_fiber);

$still_running = null;
do {
    $status = curl_multi_exec($mh, $still_running);
} while ($still_running);

do {
    /**
     * at this moment curl in fiber already finished (maybe)
     * so we must refresh $still_running variable with one more cycle "do while" in fiber
     **/
    $status_fiber = $fiber->resume();
} while (!$fiber->isTerminated());

foreach ($curlHandles as $index => $ch) {
    $index > $halfOfList ? curl_multi_remove_handle($mh_fiber, $ch) : curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
curl_multi_close($mh_fiber);
?>
viczbk.ru
пред 18 години
http://curl.haxx.se/libcurl/c/libcurl-multi.html

"When you've added the handles you have for the moment (you can still add new ones at any time), you start the transfers by call curl_multi_perform(3).

curl_multi_perform(3) is asynchronous. It will only execute as little as possible and then return back control to your program. It is designed to never block. If it returns CURLM_CALL_MULTI_PERFORM you better call it again soon, as that is a signal that it still has local data to send or remote data to receive."

So it seems the loop in sample script should look this way:

<?php
$running=null;
//execute the handles
do {
    while (CURLM_CALL_MULTI_PERFORM === curl_multi_exec($mh, $running));
    if (!$running) break;
    while (($res = curl_multi_select($mh)) === 0) {};
    if ($res === false) {
        echo "<h1>select error</h1>";
        break;
    }
} while (true);
?>

This worked fine (PHP 5.2.5 @ FBSD 6.2) without running non-blocked loop and wasting CPU time.

However this seems to be the only use of curl_multi_select, coz there's no simple way to bind it with other PHP wrappers for select syscall.
Тимо Хуовинен
пред 11 години
I tried Daniel G Zylberberg's function and
it was not working the way it was posted.
I made some changes to get it work and here is what I use:

function multiCurl($res, $options=""){

        if(count($res)<=0) return False;

        $handles = array();

        if(!$options) // add default options
            $options = array(
                CURLOPT_HEADER=>0,
                CURLOPT_RETURNTRANSFER=>1,
            );

        // add curl options to each handle
        foreach($res as $k=>$row){
            $ch{$k} = curl_init();
            $options[CURLOPT_URL] = $row['url'];
            $opt = curl_setopt_array($ch{$k}, $options);
            var_dump($opt);
            $handles[$k] = $ch{$k};
        }

        $mh = curl_multi_init();

        // add handles
        foreach($handles as $k => $handle){
            $err = curl_multi_add_handle($mh, $handle);            
        }

        $running_handles = null;

        do {
          curl_multi_exec($mh, $running_handles);
          curl_multi_select($mh);
        } while ($running_handles > 0);
       
        foreach($res as $k=>$row){
            $res[$k]['error'] = curl_error($handles[$k]);
            if(!empty($res[$k]['error']))
                $res[$k]['data']  = '';
            else
                $res[$k]['data']  = curl_multi_getcontent( $handles[$k] );  // get results

            // close current handler
            curl_multi_remove_handle($mh, $handles[$k] );
        }
        curl_multi_close($mh);
        return $res; // return response
}
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.