PHPackages                             babytree-com/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-com/httpclient

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

babytree-com/httpclient
=======================

REAL async http/https client for php

0.0.0(7y ago)08PHP

Since May 29Pushed 7y agoCompare

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

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

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

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

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

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

特性
--

[](#特性)

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

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

依赖
--

[](#依赖)

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

安装
--

[](#安装)

```
composer install babytree-com/httpclient 1.0.0
```

使用方法
----

[](#使用方法)

### 基础用法

[](#基础用法)

```
use BPF\HttpClient\Psr\RequestOptions;
use BPF\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 BPF\HttpClient\Psr\RequestOptions;
use BPF\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 BPF\HttpClient\Psr\RequestOptions;
use BPF\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 ./
```

选项
--

[](#选项)

TODO: 列出选项，如果有必要，可以写个例子

范例
--

[](#范例)

### 上传文件

[](#上传文件)

TODO: 上传文件的例子

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

2583d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8029b5a0f70b0572f8f3d3d4b1d1a0d6c02149c7847a5cc162ea0ea6079425bc?d=identicon)[babytreecom](/maintainers/babytreecom)

---

Top Contributors

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

### Embed Badge

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

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

###  Alternatives

[cboden/ratchet

PHP WebSocket library

6.4k22.0M252](/packages/cboden-ratchet)[ccxt/ccxt

A cryptocurrency trading API with more than 100 exchanges in JavaScript / TypeScript / Python / C# / PHP / Go

42.9k337.6k1](/packages/ccxt-ccxt)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78127.2M452](/packages/react-http)[ratchet/pawl

Asynchronous WebSocket client

6169.6M233](/packages/ratchet-pawl)[gos/web-socket-bundle

Symfony Web Socket Bundle

6102.2M8](/packages/gos-web-socket-bundle)[discord-php/http

Handles HTTP requests to Discord servers

24345.9k11](/packages/discord-php-http)

PHPackages © 2026

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