PHPackages                             friendsofhyperf/http-client - 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. friendsofhyperf/http-client

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

friendsofhyperf/http-client
===========================

The http client component for Hyperf, from Laravel.

v3.2.1(3w ago)546.5k↓19.2%1MITPHP

Since May 21Pushed 3w ago2 watchersCompare

[ Source](https://github.com/friendsofhyperf/http-client)[ Packagist](https://packagist.org/packages/friendsofhyperf/http-client)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/friendsofhyperf-http-client/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (20)Versions (164)Used By (1)

HTTP Client
===========

[](#http-client)

[中文说明](README_CN.md)

An HTTP client component for Hyperf, ported from Laravel and backed by Guzzle.

Installation
------------

[](#installation)

```
composer require friendsofhyperf/http-client guzzlehttp/guzzle:^7.6
```

`guzzlehttp/guzzle` is a suggested dependency of this package and is required to send requests. No configuration file needs to be published.

Sending Requests
----------------

[](#sending-requests)

Use the `Http` facade to send `GET`, `HEAD`, `POST`, `PUT`, `PATCH`, and `DELETE`requests. Request bodies are sent as JSON by default.

```
use FriendsOfHyperf\Http\Client\Http;

$response = Http::get('https://example.com/users', [
    'page' => 1,
]);

$response = Http::post('https://example.com/users', [
    'name' => 'Taylor',
]);
```

Configure a pending request by chaining methods before the HTTP verb:

```
$response = Http::baseUrl('https://example.com')
    ->withToken('token')
    ->acceptJson()
    ->timeout(10)
    ->connectTimeout(3)
    ->withHeaders(['X-Trace-Id' => 'trace-id'])
    ->withUrlParameters(['user' => 1])
    ->get('/users/{user}', ['active' => true]);
```

Use `withUrlParameters()` to expand URI template placeholders. Use `asForm()`, `attach()`, or `withBody()` for form, multipart, or raw request bodies:

```
$response = Http::withUrlParameters(['user' => 1])
    ->get('https://example.com/users/{user}');

$response = Http::asForm()->post('https://example.com/login', [
    'email' => 'taylor@example.com',
]);

$response = Http::attach('avatar', fopen('/path/to/avatar.jpg', 'r'), 'avatar.jpg')
    ->post('https://example.com/users/1/avatar');
```

The default connect timeout is 10 seconds and the default request timeout is 30 seconds. Other Guzzle request options may be supplied with `withOptions()`. Use `withoutVerifying()` only when intentionally disabling TLS certificate verification.

Retries
-------

[](#retries)

`retry()` accepts either the total number of attempts or an array of backoff delays in milliseconds. Its second argument may be a fixed delay or a closure. The optional third argument decides whether a failed response or connection exception should be retried.

```
use FriendsOfHyperf\Http\Client\PendingRequest;
use Throwable;

$response = Http::retry(
    [100, 200],
    0,
    fn (Throwable $exception, PendingRequest $request) => true,
)->get('https://example.com');
```

By default, exhausted retries throw an exception. Pass `false` as the fourth argument to return the final failed response instead.

Responses and Errors
--------------------

[](#responses-and-errors)

Requests return `FriendsOfHyperf\Http\Client\Response`. Common response methods include:

```
$response->body();
$response->json();
$response->json('user.name');
$response->object();
$response->collect();
$response->fluent();
$response->header('Content-Type');
$response->headers();
$response->status();
$response->effectiveUri();

$response->successful();
$response->redirect();
$response->failed();
$response->clientError();
$response->serverError();
```

HTTP 4xx and 5xx responses do not throw by default. Call `throw()` on the pending request or response to throw `RequestException`. Connection failures throw `ConnectionException`.

```
use FriendsOfHyperf\Http\Client\RequestException;

try {
    $response = Http::get('https://example.com/users/1')->throw();
} catch (RequestException $exception) {
    $response = $exception->response;
}
```

Concurrent Requests
-------------------

[](#concurrent-requests)

Use `pool()` to send requests concurrently. Use `as()` to assign response keys.

```
use FriendsOfHyperf\Http\Client\Http;
use FriendsOfHyperf\Http\Client\Pool;

$responses = Http::pool(function (Pool $pool) {
    return [
        $pool->as('user')->get('https://example.com/users/1'),
        $pool->as('posts')->get('https://example.com/posts'),
    ];
});

$responses['user']->status();
```

Testing
-------

[](#testing)

`fake()` intercepts matching requests and records them for assertions. Call `preventStrayRequests()` to reject requests without a matching fake.

```
use FriendsOfHyperf\Http\Client\Http;
use FriendsOfHyperf\Http\Client\Request;

Http::preventStrayRequests();
Http::fake([
    'example.com/users/*' => Http::response(['name' => 'Taylor'], 200),
]);

$response = Http::get('https://example.com/users/1');

Http::assertSent(fn (Request $request) => $request->url() === 'https://example.com/users/1');
Http::assertSentCount(1);
```

Use a response sequence when repeated requests should receive different responses. An exhausted sequence throws `OutOfBoundsException` unless `whenEmpty()` or `dontFailWhenEmpty()` is configured.

```
Http::fakeSequence('example.com/*')
    ->push(['result' => 'first'])
    ->pushStatus(404);
```

Other available assertions include `assertNotSent()`, `assertNothingSent()`, `assertSentInOrder()`, and `assertSequencesAreEmpty()`.

Middleware and Events
---------------------

[](#middleware-and-events)

Add per-request middleware with `withMiddleware()`, `withRequestMiddleware()`, or `withResponseMiddleware()`. `Factory` also provides `globalMiddleware()`, `globalRequestMiddleware()`, and `globalResponseMiddleware()`.

When the `Factory` is created with a PSR event dispatcher, it dispatches `RequestSending`, `ResponseReceived`, and `ConnectionFailed` events.

Laravel Compatibility
---------------------

[](#laravel-compatibility)

The API is based on Laravel's HTTP client, but the available API and behavior are defined by this component's source code. Consult the [Laravel HTTP Client documentation](https://laravel.com/docs/9.x/http-client) for additional background, and verify APIs against this component before using them.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance95

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 96.9% 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 ~9 days

Recently: every ~47 days

Total

163

Last Release

24d ago

Major Versions

v2.0.17 → v3.0.0-beta422022-07-28

v2.0.18 → v3.0.0-rc.22022-08-11

v2.0.20 → v3.0.0-rc.142022-09-20

v2.0.24 → v3.0.0-rc.372022-12-09

2.0.x-dev → v3.0.32023-01-18

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8337659?v=4)[Deeka Wong](/maintainers/huangdijia)[@huangdijia](https://github.com/huangdijia)

---

Top Contributors

[![huangdijia](https://avatars.githubusercontent.com/u/8337659?v=4)](https://github.com/huangdijia "huangdijia (124 commits)")[![xuanyanwow](https://avatars.githubusercontent.com/u/28777109?v=4)](https://github.com/xuanyanwow "xuanyanwow (3 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")

---

Tags

hyperfv3.2

### Embed Badge

![Health badge](/badges/friendsofhyperf-http-client/health.svg)

```
[![Health](https://phpackages.com/badges/friendsofhyperf-http-client/health.svg)](https://phpackages.com/packages/friendsofhyperf-http-client)
```

###  Alternatives

[hyperf/database

A flexible database library.

192.9M321](/packages/hyperf-database)[hyperf/http-server

A HTTP Server for Hyperf.

103.0M371](/packages/hyperf-http-server)[hyperf/validation

hyperf validation

122.2M197](/packages/hyperf-validation)[hyperf/crontab

A crontab component for Hyperf.

131.7M77](/packages/hyperf-crontab)[hyperf/amqp

A amqplib for hyperf.

231.3M70](/packages/hyperf-amqp)[hyperf/database-pgsql

A pgsql handler for hyperf/database.

12321.2k21](/packages/hyperf-database-pgsql)

PHPackages © 2026

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