定制 gemvc/http-client 二次开发

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

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

gemvc/http-client

Composer 安装命令:

composer require gemvc/http-client

包简介

Multi-Environment PHP HTTP Client Package - Synchronous and Asynchronous API Calls for Apache, Nginx, and Swoole

README 文档

README

gemvc_let

gemvc/http-client

Multi-Environment PHP HTTP Client Package
Synchronous and Asynchronous API Calls for Apache, Nginx, and Swoole

Part of Gemvc , a lightweight PHP Framework for microservices

Overview

A framework-independent HTTP client package providing both synchronous and asynchronous API call capabilities. Automatically adapts to your runtime environment (Apache, Nginx, or Swoole) for optimal performance.

Features

  • Synchronous API calls with retry logic and SSL support
  • Asynchronous concurrent requests with configurable concurrency
  • Enhanced error handling with exception storage and classification
  • Environment-aware execution - optimized for each runtime (Native Coroutines for Swoole)
  • Framework-independent - use in any PHP project
  • Backward compatible with GEMVC framework

Installation

composer require gemvc/http-client

Requirements

  • PHP 8.2 or higher
  • cURL extension
  • OpenSwoole extension (optional, for optimized async in Swoole)

Quick Start

Synchronous Client

use Gemvc\Http\Client\HttpClient;

$client = new HttpClient();
$client->setTimeouts(10, 30)
       ->setRetries(3, 200, [500, 502, 503]);

$response = $client->get('https://api.example.com/users', ['page' => 1]);
$data = $client->post('https://api.example.com/users', ['name' => 'John']);

// Error handling (exceptions enabled by default)
try {
    $response = $client->get('https://api.example.com/data');
} catch (\Gemvc\Http\Client\Exception\NetworkException $e) {
    echo "Network error: {$e->getMessage()}\n";
    if ($e->isDnsError()) {
        echo "DNS resolution failed\n";
    }
}

// Or use error storage (disable exceptions)
$client->throwExceptions(false);
$response = $client->get('https://api.example.com/data');
if ($client->hasErrors()) {
    $error = $client->getLastError();
    echo "Error: {$error->getMessage()}\n";
}

Asynchronous Client

use Gemvc\Http\Client\AsyncHttpClient;

$async = new AsyncHttpClient();
$async->setMaxConcurrency(5)
      ->setTimeouts(10, 30);

$async->addGet('users', 'https://api.example.com/users', ['page' => 1])
      ->addGet('posts', 'https://api.example.com/posts', ['limit' => 10])
      ->addPost('create', 'https://api.example.com/create', ['name' => 'Test']);

$results = $async->executeAll();

foreach ($results as $requestId => $result) {
    if ($result['success']) {
        echo "Request {$requestId}: {$result['body']}\n";
    }
}

Fire-and-Forget (Non-Blocking)

use Gemvc\Http\Client\AsyncHttpClient;

// Perfect for APM logging, analytics, or background tasks
$apm = new AsyncHttpClient();
$apm->setTimeouts(2, 5)
    ->addPost('apm-log', 'https://apm.example.com/log', [
        'endpoint' => '/api/User/list',
        'duration' => 0.123,
        'status' => 200
    ])
    ->fireAndForget(); // Does NOT block!

Environment-Specific Implementations

The package provides environment-specific implementations:

  • HttpClient - Apache/Nginx synchronous implementation
  • AsyncHttpClient - Apache/Nginx asynchronous implementation
  • SwooleHttpClient - Native Swoole implementation using Coroutines (no cURL dependency)

API Reference

HttpClient Methods

// HTTP Methods
get(string $url, array $queryParams = []): string|false
post(string $url, array $data = []): string|false
put(string $url, array $data = []): string|false
postForm(string $url, array $fields = []): string|false
postMultipart(string $url, array $fields = [], array $files = []): string|false
postRaw(string $url, string $body, string $contentType): string|false

// Configuration
setTimeouts(int $connectTimeout, int $timeout): self
setSsl(?string $cert, ?string $key, ?string $ca = null, bool $verifyPeer = true, int $verifyHost = 2): self
setRetries(int $maxRetries, int $retryDelayMs = 200, array $retryOnHttpCodes = []): self
retryOnNetworkError(bool $retry): self
setUserAgent(string $userAgent): self
throwExceptions(bool $throw): self

// Error Handling
clearErrors(): self
hasErrors(): bool
getErrors(): array<HttpClientException>
getLastError(): ?HttpClientException

AsyncHttpClient Methods

// Request Building
addGet(string $requestId, string $url, array $queryParams = [], array $headers = []): self
addPost(string $requestId, string $url, array $data = [], array $headers = []): self
addPut(string $requestId, string $url, array $data = [], array $headers = []): self
addPostForm(string $requestId, string $url, array $fields = [], array $headers = []): self
addPostMultipart(string $requestId, string $url, array $fields = [], array $files = [], array $headers = []): self
addPostRaw(string $requestId, string $url, string $body, string $contentType, array $headers = []): self

// Execution
executeAll(): array<string, array{success: bool, body: string|false, http_code: int, error: string, duration: float, exception: HttpClientException|null, exception_type: string|null}>
fireAndForget(): bool
waitForAll(): array

// Configuration
setMaxConcurrency(int $max): self
setTimeouts(int $connectTimeout, int $timeout): self
setSsl(?string $cert, ?string $key, ?string $ca = null, bool $verifyPeer = true, int $verifyHost = 2): self
setUserAgent(string $userAgent): self
onResponse(string $requestId, callable $callback): self
clearQueue(): self
getQueueSize(): int

// Error Handling
clearErrors(): self
hasErrors(): bool
getErrors(): array<HttpClientException>
getLastError(): ?HttpClientException

Framework Integration (GEMVC)

In GEMVC framework, the package is automatically used via wrapper classes:

// Framework automatically selects implementation based on environment
$api = new \Gemvc\Http\ApiCall(); // Uses package internally
$api->get('https://api.example.com/data');

The framework provides:

  • Environment detection via WebserverDetector
  • Seamless integration with existing ApiCall and AsyncApiCall classes
  • 100% backward compatibility

Error Handling

The package provides comprehensive error handling with automatic exception classification:

Exception Types

  • HttpClientException - Base exception for all HTTP client errors
  • NetworkException - Network-related errors (DNS, connection, SSL, etc.)
  • TimeoutException - Request timeout errors (connection or total timeout)

Error Storage

All exceptions are automatically stored in the $errors array property, allowing you to inspect errors without try-catch blocks:

$client = new HttpClient();
$client->throwExceptions(false); // Store errors instead of throwing

$response = $client->get('https://api.example.com/data');

if ($client->hasErrors()) {
    $error = $client->getLastError();
    // Access error details: $error->getUrl(), $error->getHttpCode(), etc.
}

Network Error Classification

Network errors are automatically classified by type:

try {
    $client->get('https://api.example.com/data');
} catch (NetworkException $e) {
    if ($e->isDnsError()) {
        // DNS resolution failed
    } elseif ($e->isSslError()) {
        // SSL/TLS handshake failed
    } elseif ($e->isConnectionError()) {
        // Connection failed
    }
    
    // Get error type description
    echo $e->getErrorTypeDescription();
}

Testing

composer test

Test coverage: 85.30% with comprehensive error handling tests.

License

MIT License - See LICENSE file for details

Contributing

Contributions welcome! Please follow:

  • PSR-12 coding standards
  • PHPStan Level 9 type safety
  • Comprehensive test coverage

GemVC , Lightweight PHP Framework Built for Microservices Made with ❤️ by Ali Khorsandfard

gemvc/http-client 适用场景与选型建议

gemvc/http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「curl」 「http」 「api」 「client」 「async」 「apache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 gemvc/http-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gemvc/http-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 gemvc/http-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-18