一个用于更轻松地使用 [curl_multi_exec](https://secure.php.net/manual/en/function.curl-multi-exec.php) 发送异步curl请求的包。

1.1.1 2020-12-04 14:32 UTC

This package is auto-updated.

Last update: 2024-09-04 23:00:04 UTC


README

一个用于更轻松地使用 curl_multi_exec 发送异步curl请求的包。

示例

基本批量请求

在这里我们将创建1,000个请求并将它们全部发送。当所有请求都返回后,我们将处理响应。

$curlHandler = new Programster\AsyncCurl\BasicRequestHandler();

// create 1,000 requests to fire off asynchronously
for ($i=0; $i<1000; $i++)
{
    $params = array("request_id" => $i);
    $headers = array("header1" => "value1");

    $request = new \Programster\AsyncCurl\BasicRequest(
        "https://:8081",
        Programster\AsyncCurl\Method::createPost(),
        $timeout=5, // seconds
        $params,
        $headers
    );

    $curlHandler->add($request);
}

// Fire the requests and get the responses
$curlHandler->run();

// get the responses array and do something with them
$responses = $curlHandler->getResponses();

foreach ($responses as $response)
{
    /* @var $response Programster\AsyncCurl\Response */
    // do something
}

异步处理返回的响应

在这里我们将创建1,000个请求,但我们不会等待所有响应返回后再处理它们,而是立即处理。这依赖于我们创建一个“处理程序”回调,该回调将处理响应。

$curlHandler = new Programster\AsyncCurl\AsyncRequestHandler();

// create 1,000 requests
for ($i=0; $i<1000; $i++)
{
    $params = array("request_id" => $i);
    $headers = array("header1" => "value1");

    // create the handler that will handle the response immediately after it comes back.
    $handler = function(Programster\AsyncCurl\Response $response) {
        if ($response->hadCurlError() === false)
        {
            print "Request went through ok: " . PHP_EOL;
            print "response code: " . $response->getHttpCode() . PHP_EOL;
            print "handling response " . $response->getResponseBody() . PHP_EOL;
        }
        else
        {
            print "There was an issue with the request: " . PHP_EOL;
            print "error code: " . $response->getCurlErrorCode() . PHP_EOL;
            print "error message: " . $response->getCurlErrorMessage() . PHP_EOL;
        }
    };

    $request = new \Programster\AsyncCurl\AsyncRequest(
        "https://:8081",
        Programster\AsyncCurl\Method::createPost(),
        $timeout=5,
        $params,
        $headers,
        $handler
    );

    $curlHandler->add($request);
}

// Have the curl handler fire off the requests. Here it is non-blocking so you could do other
// things while the requests are being sent/recieved.
while ($curlHandler->getState() !== Programster\AsyncCurl\AsyncRequestHandler::SATE_COMPLETED)
{
    $curlHandler->run();
    // possibly do other things...?
    usleep(10); // don't waste CPU
}

// After getting out of the while loop, all requests will have had their handlers run against
// their response, but you can still get the responses from the handler should you desire...
$responses = $curlHandler->getResponses();