webservco/http-client

一个PHP组件/库。

This package is auto-updated.

Last update: 2024-08-25 16:11:49 UTC


README

使用cURL实现的简约PHP HTTP客户端实现。

测试项目: webservco/http-client-test

安装

composer.json:

{
  "require": {
    "webservco/http-client": "^0"  
  }
}

使用

PSR-18 HTTP客户端实现

  • 常规使用(发送请求,接收响应);
  • 完全符合PSR规范(可以作为直接替换使用);
// PSR-18: \Psr\Http\Client\ClientInterface
$httpClient = new HttpClient(
    // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
    $curlService,
);
$response = $httpClient->sendRequest(
    // PSR-7: \Psr\Http\Message\RequestInterface
    $request,
);

自定义多进程HTTP客户端实现

  • 用于通过cURL多处理发送多个并行请求;
  • 自定义实现,尽管输入和输出都符合PSR规范;
// \WebServCo\Http\Client\Contract\Service\cURL\CurlMultiServiceInterface
$curlMultiService = new CurlMultiService(
    // \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
    $curlService
);

/** @var array<int,\Psr\Http\Message\RequestInterface> $requests */
$requests = [
    // PSR-7: \Psr\Http\Message\RequestInterface
    1 => $request1,
    // PSR-7: \Psr\Http\Message\RequestInterface
    2 => $request2,
    // etc
];

// Keep a list of handles to be able to link them to each id. key: id, value: handle identifier.
/** @var array<int,string> $curlHandleIdentifiers */
$curlHandleIdentifiers = [];

foreach ($requests as $request) {
    // Create handle and add it's identifier to the list.
    $handleIdentifier = $curlMultiService->createHandle($request);
    $curlHandleIdentifiers[$releaseId] = $handleIdentifier;
}

// Add more requests if needed
// Create handle and add it's identifier to the list.
$handleIdentifier = $curlMultiService->createHandle($request);
$curlHandleIdentifiers[$releaseId] = $handleIdentifier;

// When ready, execute all requests in parallel
$curlMultiService->executeSessions();

// Retrieve responses

// Use case: consumer keeps track of requests added and needs to be able to identify corresponding responses.
foreach ($curlHandleIdentifiers as $releaseId => $handleIdentifier) {
    $response = $curlMultiService->getResponse($handleIdentifier);
    
    // Do something with the response
}

// Use case: consumer just needs all responses, no need to link them to the requests.
foreach ($curlMultiService->iterateResponse() as $response) {
    // Do something with the response
}

// Cleanup. After this the service can be re-used, going through all the steps.
$curlMultiService->cleanup();

初始化

CurlServiceInterface

主服务类,任何选择的使用都需要。

依赖:任何PSR-17实现都可以使用。

// \WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface
$curlService = new CurlService(
    // \WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration
    new CurlServiceConfiguration(
        // enableDebugMode
        true
    ),
    // \WebServCo\Log\Contract\LoggerFactoryInterface
    new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath)),
    // PSR-17: \Psr\Http\Message\ResponseFactoryInterface
    $responseFactory,
    // PSR-17: \Psr\Http\Message\StreamFactoryInterface
    $streamFactory,
);

使用webservco/http实现的一个示例工厂

<?php

declare(strict_types=1);

namespace Project\Http\Client\Factory;

use WebServCo\Http\Client\Contract\Service\cURL\CurlServiceInterface;
use WebServCo\Http\Client\DataTransfer\CurlServiceConfiguration;
use WebServCo\Http\Client\Service\cURL\CurlService;
use WebServCo\Http\Factory\Message\Response\ResponseFactory;
use WebServCo\Http\Factory\Message\Stream\StreamFactory;
use WebServCo\Http\Service\Message\Response\StatusCodeService;
use WebServCo\Log\Contract\LoggerFactoryInterface;

final class CurlServiceFactory
{
    public function __construct(private LoggerFactoryInterface $loggerFactory)
    {
    }

    public function createCurlService(int $timeout): CurlServiceInterface
    {
        $streamFactory = new StreamFactory();

        return new CurlService(
            new CurlServiceConfiguration(true, $timeout),
            $this->loggerFactory,
            new ResponseFactory(new StatusCodeService(), $streamFactory),
            $streamFactory,
        );
    }
}
$loggerFactory = new ContextFileLoggerFactory(sprintf('%svar/log', $projectPath));
new CurlServiceFactory($loggerFactory);

贡献

欢迎Pull请求。对于重大更改,请先打开一个问题来讨论您想进行哪些更改。

请确保适当地更新测试。

许可证

MIT