定制 webservco/http-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

webservco/http-client

最新稳定版本:v1.0.0

Composer 安装命令:

composer require webservco/http-client

包简介

A PHP component/library.

README 文档

README

A minimalist PHP HTTP Client implementation using cURL.

Test project: webservco/http-client-test

Installation

composer.json:

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

Usage

PSR-18 HTTP client implementation

  • Regular usage (send a request, receive a response);
  • Fully PSR compliant (can be used as a drop-in replacement);
// 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,
);

Custom multi processing HTTP client implementation

  • Use to send multiple requests in parallel via cURL multi handle;
  • Custom implementation, though input and output are PSR compliant;
// \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();

Initialization

CurlServiceInterface

Main service class, needed for any chosen usage.

Dependencies: any PSR-17 implementation can be used.

// \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,
);

An example factory using webservco/http implementation

<?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);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

统计信息

  • 总下载量: 299
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-09