定制 jbzoo/http-client 二次开发

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

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

jbzoo/http-client

Composer 安装命令:

composer require jbzoo/http-client

包简介

Simple HTTP-client, wrapper around Guzzle and rmccue/requests

README 文档

README

CI Coverage Status Psalm Coverage Psalm Level CodeFactor Stable Version Total Downloads Dependents GitHub License

A simple, intuitive PHP HTTP client that provides a clean wrapper around popular HTTP libraries like Guzzle and rmccue/requests. Make HTTP requests with minimal code and maximum flexibility.

Features

  • Simple API: Clean, one-line HTTP requests without complex configuration
  • Multiple Backends: Automatic driver selection (Guzzle preferred, rmccue/requests fallback)
  • Parallel Requests: Built-in support for concurrent HTTP requests using curl_multi_*
  • Flexible Response Handling: Access response data via methods, properties, or array syntax
  • JSON Support: Built-in JSON parsing with JBZoo/Data integration
  • Event System: Hook into request lifecycle with event listeners
  • PHP 8.2+ Ready: Modern PHP with strict typing and best practices

Requirements

  • PHP 8.2 or higher
  • ext-json

Installation

composer require guzzlehttp/guzzle --no-update # Recommended, but not required
composer require jbzoo/http-client

Quick Start

use JBZoo\HttpClient\HttpClient;

// Simple GET request
$client = new HttpClient();
$response = $client->request('https://api.github.com/users/octocat');

echo $response->getBody(); // JSON response
echo $response->getCode(); // 200

Usage

use JBZoo\HttpClient\HttpClient;

// Configure client (no options required!)
$httpClient = new HttpClient([
    'auth'            => [          // Simple HTTP auth
        'http-user-name',
        'http-password'
    ],
    'headers'         => [          // Your custom headers
        'X-Custom-Header' => 42,
    ],
    'driver'          => 'auto',    // (Auto|Guzzle5|Guzzle6|Rmccue)
    'timeout'         => 10,        // Wait in seconds
    'verify'          => false,     // Check cert for SSL
    'exceptions'      => false,     // Show exceptions for statuses 4xx and 5xx
    'allow_redirects' => true,      // Show real 3xx-header or result?
    'max_redirects'   => 10,        // How much to redirect?
    'user_agent'      => "It's me", // Custom UserAgent
]);

// Just request
$response = $httpClient->request('http://my.site.com/', [
    'key-1' => 'value-1',
    'key-2' => 'value-2'
], 'post');

Response Methods

// Get status code
$code = $response->getCode();
$code = $response->code;
$code = $response['code'];

// Get headers
$headers = $response->getHeaders();
$headers = $response->headers;
$headers = $response['headers'];
$header  = $response->getHeader('X-Custom-Header-Response');
$header  = $response->find('headers.x-custom-header-response', 'default-value', 'trim');

// Get response body
$body = $response->getBody();
$body = $response->body;
$body = $response['body'];

// Parse JSON response (uses JBZoo/Data)
$json = $response->getJSON();
$value = $json->get('key', 'default', 'trim');
$value = $json->find('key.nested', 'default', 'trim');

Parallel Requests

use JBZoo\HttpClient\HttpClient;

$httpClient = new HttpClient();

$results = $httpClient->multiRequest(array(
    'request_0' => 'http://mockbin.org/request',

    'request_1' => ['http://mockbin.org/request', [
        'args' => ['key' => 'value']
    ]],

    'request_2' => ['http://mockbin.org/request', [
        'method' => 'post',
        'args'   => ['key' => 'value'],
        'headers'         => [
            'X-Custom-Header' => 42,
        ],
        'timeout'         => 10,
        'verify'          => false,
        'exceptions'      => false,
        'allow_redirects' => true,
        'max_redirects'   => 10,
        'user_agent'      => 'JBZoo/Http-Client v1.x-dev'
    ]]
]);

$results['request_0']->getBody();
$results['request_1']->getBody();
$results['request_2']->getBody();

Development

Running Tests

make update          # Install/update dependencies
make test-all        # Run tests and code style checks
make test            # Run PHPUnit tests only
make codestyle       # Run code style checks only

Mock Server

For testing purposes, you can start a mock HTTP server:

make start-mock-server  # Starts httpbin on port 8087

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Run tests (make test-all)
  4. Commit your changes (git commit -am 'Add amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

License

MIT - see LICENSE file for details.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 148.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 18
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-07-31