bluepsyduck/multicurl

一个简单的库,帮助使用多 cUrl 一次性请求多个资源。

2.0.1 2017-11-29 20:18 UTC

This package is auto-updated.

Last update: 2024-09-12 07:37:22 UTC


README

Latest Stable Version Total Downloads License Build Status codecov

此库通过 PHP 的 Multi-cURL 函数帮助创建和执行多个请求。

要求

  • PHP 7.0 或更高版本
  • PHP cURL 扩展

使用方法

该库的主要类是 MultiCurlManager,您可以添加任意多的请求,它将执行这些请求。对于每个要开始的请求,创建一个 Entity\Request 类的实例,并根据需要设置其属性,然后使用 $manager->addRequest($request) 将其添加到管理器中。添加的请求将立即执行,而不会阻塞脚本执行。

要等待某个请求完成,请调用 $manager->waitForSingleRequest($request)。要等待所有请求完成,请调用 $manager->waitForAllRequests()。这些方法将阻塞脚本执行,直到所需的请求完成。

一旦请求完成,使用 $request->getResponse() 获取响应信息。您可能需要检查 $response->getErrorCode()$response->getErrorMessage() 以获取请求失败时的任何信息。

请求提供了两个回调函数

  • onInitialize:此回调在底层 cURL 请求初始化后触发。使用此回调进一步操作 cURL。在此回调之后,cURL 请求将被执行。
  • onComplete:此回调在 cURL 请求完成并且响应被解析到实体中后触发。

示例

以下是一个基本示例,演示了如何使用 MultiCurlManager

<?php

use BluePsyduck\MultiCurl\MultiCurlManager;
use BluePsyduck\MultiCurl\Entity\Request;

$manager = new MultiCurlManager();

$requestFoo = new Request();
$requestFoo->setUrl('http://localhost/data.php?action=foo');
$manager->addRequest($requestFoo); // Will execute the first request, but will not wait for it to finish.

$requestBar = new Request();
$requestBar->setUrl('http://localhost/data.php?action=bar');

$manager->addRequest($requestBar); // Will execute the second request, having both run parallel.

// Some other code.

$manager->waitForAllRequests(); // Will wait for both requests to be finished.

// Do something with the responses
var_dump($requestFoo->getResponse());
var_dump($requestBar->getResponse());

以下是一个示例,演示了如何限制并行请求的数量

<?php 

use BluePsyduck\MultiCurl\MultiCurlManager;
use BluePsyduck\MultiCurl\Entity\Request;

$manager = new MultiCurlManager();
$manager->setNumberOfParallelRequests(4); // Limit number of parallel requests.

for ($i = 0; $i < 16; ++$i) {
    $request = new Request();
    $request->setUrl('http://localhost/data.php?i=' . $i);
    $request->setOnInitializeCallback(function(Request $request) use ($i) {
        echo 'Initialize #' . $i . PHP_EOL;
    });
    $request->setOnCompleteCallback(function(Request $request) use ($i) {
        echo 'Complete #' . $i . PHP_EOL;
    });
    $manager->addRequest($request);
}

// Now 16 requests have been scheduled to be executed, but only 4 requests will run in parallel.
// Once a request finishes, the next one will be executed.

$importantRequest = new Request();
$importantRequest->setUrl('http://localhost/data.php?type=important');
$importantRequest->setOnInitializeCallback(function(Request $request) use ($i) {
    echo 'Initialize important request' . PHP_EOL;
});
$importantRequest->setOnCompleteCallback(function(Request $request) use ($i) {
    echo 'Complete important request' . PHP_EOL;
});
$manager->addRequest($importantRequest); // Important request will not be executed because of the limit.

$manager->waitForSingleRequest($importantRequest); // This will execute the request, ignoring the limit.
// So now actually 5 requests are running in parallel.

echo 'Important request has finished.' . PHP_EOL;

$manager->waitForAllRequests(); // Wait for all the other requests.