PHPackages                             babytree/httpclient - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. babytree/httpclient

ActiveLibrary[HTTP &amp; Networking](/categories/http)

babytree/httpclient
===================

REAL async http/https client for php

1.0.0(6y ago)11202PHP

Since Jun 5Pushed 6y ago1 watchersCompare

[ Source](https://github.com/babytree/httpclient)[ Packagist](https://packagist.org/packages/babytree/httpclient)[ RSS](/packages/babytree-httpclient/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

PHP异步http/https客户端
==================

[](#php异步httphttps客户端)

绝大部分互联网公司的php-fpm都跑在单进程单线程模式下，随着微服务架构的兴起，普通的curl已经不能满足复杂业务场景的需求。而curl\_multi和guzzle尽管支持多个http请求的异步执行，但是它们的api对后端开发同学并不友好。

babytree/httpclient是宝宝树在复杂业务场景下积累的php http客户端。

特性
--

[](#特性)

- http请求和业务代码可以异步执行
- 多个http请求可以异步执行
- 满足psr规范
- 比guzzle、curl\_multi更友好的api
- 经过了线上复杂业务场景的考验

其中，http请求和业务代码的异步执行，是curl\_multi和guzzle所不支持的。使用httpclient，在复杂业务场景下，可以将总体代码运行时间进一步缩短，进而提高QPS。

依赖
--

[](#依赖)

- php 7.0+
- 如果要进行单元测试，需要安装phpunit和go

安装
--

[](#安装)

```
composer require babytree/httpclient 1.0.0
```

使用方法
----

[](#使用方法)

### 基础用法

[](#基础用法)

```
use Babytree\HttpClient\Psr\RequestOptions;
use Babytree\HttpClient\RequestClient;

$options = array(
    // some options
);
$request_uniq = $request_client->addRequest($some_api, $options, RequestClient::MODE_ASYNC);
$ret = $request_client->getResponse($request_uniq);

// 对请求结果进行业务操作
// ...
```

### 业务逻辑和http请求异步

[](#业务逻辑和http请求异步)

```
use Babytree\HttpClient\Psr\RequestOptions;
use Babytree\HttpClient\RequestClient;

$options = array(
    // some options
);
$request_uniq = $request_client->addRequest($some_api, $options, RequestClient::MODE_ASYNC);

// 这里可以放可以和请求并行处理的业务逻辑
// some business code

$ret = $request_client->getResponse($request_uniq);

// 对请求结果进行业务操作
// ...
```

### 多个请求异步

[](#多个请求异步)

```
use Babytree\HttpClient\Psr\RequestOptions;
use Babytree\HttpClient\RequestClient;

$request_client = new RequestClient();
$options = array(
    // some options
);
$multi_urls = array(
            $api1,
            $api2,
            $api3,
            ...
        );
$request_list = array();
foreach ($multi_urls as $url) {
    $request_uniq = $request_client->addRequest($url, $options, RequestClient::MODE_ASYNC);
    $request_list[$request_uniq] = $request_uniq;
}

// 这里可以放可以和请求并行处理的业务逻辑
// some business code

do {
    $request_uniq = null;
    try {
        $ret = $request_client->selectGetAsyncResponse($request_uniq, null);
    } catch (\Exception $e) {
    }
    if ($request_uniq && isset($request_list[$request_uniq])) {
        unset($request_list[$request_uniq]);
    }
    if (!$request_list) {
        break;
    }
} while (true);

// 对请求结果进行业务操作
// ...
```

运行单元测试
------

[](#运行单元测试)

```
phpunit tests ./
```

选项
--

[](#选项)

```
$options = array(
	//请求超时时间
	RequestOptions::TIMEOUT => 3,
	//debug, $stream不指定时输出到标准设备
	RequestOptions::DEBUG  => $stream,
	//设置header
	RequestOptions::HEADERS => [
	        'timestamp'    => time() * 1000,
	        'signature'    => $signature,
	        'platform'     => 1,
	        'token'        => $meitun_token,
	    ],
	//代理
	RequestOptions::PROXY   => '172.16.99.239:8888',
	//post json格式 默认添加header 'Content-Type', 'application/json;charset=utf-8'
	RequestOptions::JSON => array(
	    'baby_id' => '11111',
	    'baby_name' => '对对对',
	    'baby_gender' => '男',
	    ),
	//post form表单格式 默认添加header 'Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'
	RequestOptions::FORM_PARAMS => array(
	    'baby_id' => '11111',
	    'baby_name' => '对对对',
	    'baby_gender' => '男',
	    ),
	//post上传文件 默认添加header 'Content-Type', 'multipart/form-data; boundary='
	RequestOptions::MULTIPART => array(
	    'id'        => 1,
	    'user_id'   => 2,
	    'svg_file1' => '/home/baiwei/poster_backgroup.png',
	    'svg_file2' => '/home/baiwei/poster_event.png',
	    ),
);
```

范例
--

[](#范例)

### 上传文件

[](#上传文件)

```
$request_client = new RequestClient();

// 如要要测试，可以使用tests/server.go提供的上传功能来作为测试服务器
$server_url = "http://127.0.0.1:18888/upload";

$options = array(
        RequestOptions::DEBUG  => 1,
        RequestOptions::MULTIPART => array(
            'id'        => 1,
            'user_id'   => 2,
            'file' => '/home/baiwei/poster_backgroup.png',
            ),
        );
$request_uniq = $request_client->addRequest($server_url, $options, RequestClient::MODE_ASYNC);
$ret = $request_client->getResponse($request_uniq);
// 对请求结果进行处理
// ...
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

2535d ago

Major Versions

0.0.0 → 1.0.02019-06-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/e6775f64e3512250cfa7ce363a29e5bceae53031bfdc9d07ece760a183724f78?d=identicon)[babytree](/maintainers/babytree)

---

Top Contributors

[![babytreecom](https://avatars.githubusercontent.com/u/50975871?v=4)](https://github.com/babytreecom "babytreecom (5 commits)")

### Embed Badge

![Health badge](/badges/babytree-httpclient/health.svg)

```
[![Health](https://phpackages.com/badges/babytree-httpclient/health.svg)](https://phpackages.com/packages/babytree-httpclient)
```

###  Alternatives

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

538204.9M23](/packages/league-uri-interfaces)[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

184616.9k31](/packages/laudis-neo4j-php-client)[http-interop/response-sender

A function to convert PSR-7 Response to HTTP output

46711.5k40](/packages/http-interop-response-sender)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
