mahelbir/mclient

为 Guzzle 提供简单的异步请求包装器

2.1 2024-04-27 18:06 UTC

This package is auto-updated.

Last update: 2024-10-01 00:14:55 UTC


README

Latest version Software License

Mclient 作为 Guzzle 的直接异步包装器,用于发起 HTTP 请求。

要求

PHP 7.1+

Guzzle 7.0+

安装

如果您使用 Composer 管理项目依赖,只需在 composer.json 文件中添加对 mahelbir/mclient 的依赖即可

composer require mahelbir/mclient

尽管推荐使用 Composer,但您实际上可以以任何方式包含文件。

用法

$mclient = new \Mahelbir\Mclient();

//Library options
$mclient->setTimeout(10); // total seconds
$mclient->setConnectTimeout(5);
$mclient->setConcurrency(100);

// Async multiple requests
$mclient->request(
    'GET',
    'https://httpbin.org/get',
    ['X-CUSTOM-HEADER' => 'Value'],
    ['proxy' => '127.0.0.1:8080'],
    'request_1_extra_data'
);
$mclient->post(
    'https://httpbin.org/post',
    ['key' => 'value'],
    ['User-Agent' => 'Googlebot'],
    ['allow_redirects' => false],
    'request_2_extra_data'
);
$mclient->post(
    'https://httpbin.org/post',
    '{"key": "value"}',
    ['content-type' => 'application/json']
);
$mclient->get(
    'https://httpbin.org/get?q=1',
    [],
    [],
    [],
    ['request_3_extra_data' => 'value_extra']
);
$mclient->get(
    'https://httpbin.org/get',
    ["q" => 2]
);

// Mclient supports all the request options available in Guzzle, with the addition of an 'interface' option (https://docs.guzzlephp.org/en/stable/request-options.html)
$mclient->get('https://google.com', [], [], ['interface' => '2001:db8:3333:4444:5555:6666:7777:8888']);

$responses = $mclient->execute();
foreach ($responses as $response) {
    $status = $response['code'];
    $body = $response['body'];
    $headers = $response['headers'];
    $request = $response['request'];
    $extra = $response['extra'];
}

// Pass true parameter to retrieve the first/single response
$mclient->get('https://httpbin.org/get');
$response = $mclient->execute(true);

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件