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

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

gokure/http-client
==================

An expressive, minimal API around the Guzzle HTTP client for Hyperf.

v1.1.1(4y ago)4485MITPHPPHP &gt;= 7.2

Since Apr 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/gokure/hyperf-http-client)[ Packagist](https://packagist.org/packages/gokure/http-client)[ RSS](/packages/gokure-http-client/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (4)Dependencies (9)Versions (8)Used By (0)

A HTTP Client for Hyperf
========================

[](#a-http-client-for-hyperf)

Implements HTTP client for Hyperf via [laravel/framework](https://github.com/laravel/framework).

Introduction
------------

[](#introduction)

HTTP Client is an expressive, minimal API around the [Guzzle HTTP client](http://docs.guzzlephp.org/en/stable/) that implements from Laravel, allowing you to quickly make outgoing HTTP requests to communicate with other web applications.

Before getting started, you should ensure that you have installed the Guzzle package as a dependency of your application. By default, Hyperf automatically includes this dependency. However, if you have previously removed the package, you may install it again via Composer:

```
composer require guzzlehttp/guzzle
```

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

[](#installation)

Require the `gokure/http-client` package in your `composer.json` and update your dependencies:

```
composer require gokure/http-client
```

Making Requests
---------------

[](#making-requests)

To make requests, you may use the `get`, `post`, `put`, `patch`, and `delete` methods provided by the `Http` facade. First, let's examine how to make a basic `GET` request to another URL:

```
use Gokure\Http\Client\Http;

$response = Http::get('http://example.com');
```

The `get` method returns an instance of `Gokure\Http\Client`, which provides a variety of methods that may be used to inspect the response:

```
$response->body() : string;
$response->json() : array|mixed;
$response->collect() : Hyperf\Utils\Collection;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;
```

The `Gokure\Http\Client\Response` object also implements the PHP `ArrayAccess` interface, allowing you to access JSON response data directly on the response:

```
return Http::get('http://example.com/users/1')['name'];
```

#### Dumping Requests

[](#dumping-requests)

If you would like to dump the outgoing request instance before it is sent and terminate the script's execution, you may add the `dd` method to the beginning of your request definition:

```
return Http::dd()->get('http://example.com');
```

### Request Data

[](#request-data)

Of course, it is common when making `POST`, `PUT`, and `PATCH` requests to send additional data with your request, so these methods accept an array of data as their second argument. By default, data will be sent using the `application/json` content type:

```
use Gokure\Http\Client\Http;

$response = Http::post('http://example.com/users', [
    'name' => 'Steve',
    'role' => 'Network Administrator',
]);
```

#### GET Request Query Parameters

[](#get-request-query-parameters)

When making `GET` requests, you may either append a query string to the URL directly or pass an array of key / value pairs as the second argument to the `get` method:

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

#### Sending Form URL Encoded Requests

[](#sending-form-url-encoded-requests)

If you would like to send data using the `application/x-www-form-urlencoded` content type, you should call the `asForm` method before making your request:

```
$response = Http::asForm()->post('http://example.com/users', [
    'name' => 'Sara',
    'role' => 'Privacy Consultant',
]);
```

#### Sending A Raw Request Body

[](#sending-a-raw-request-body)

You may use the `withBody` method if you would like to provide a raw request body when making a request. The content type may be provided via the method's second argument:

```
$response = Http::withBody(
    base64_encode($photo), 'image/jpeg'
)->post('http://example.com/photo');
```

#### Multi-Part Requests

[](#multi-part-requests)

If you would like to send files as multi-part requests, you should call the `attach` method before making your request. This method accepts the name of the file and its contents. If needed, you may provide a third argument which will be considered the file's filename:

```
$response = Http::attach(
    'attachment', file_get_contents('photo.jpg'), 'photo.jpg'
)->post('http://example.com/attachments');
```

Instead of passing the raw contents of a file, you may pass a stream resource:

```
$photo = fopen('photo.jpg', 'r');

$response = Http::attach(
    'attachment', $photo, 'photo.jpg'
)->post('http://example.com/attachments');
```

### Headers

[](#headers)

Headers may be added to requests using the `withHeaders` method. This `withHeaders` method accepts an array of key / value pairs:

```
$response = Http::withHeaders([
    'X-First' => 'foo',
    'X-Second' => 'bar'
])->post('http://example.com/users', [
    'name' => 'Taylor',
]);
```

### Authentication

[](#authentication)

You may specify basic and digest authentication credentials using the `withBasicAuth` and `withDigestAuth` methods, respectively:

```
// Basic authentication...
$response = Http::withBasicAuth('taylor@laravel.com', 'secret')->post(...);

// Digest authentication...
$response = Http::withDigestAuth('taylor@laravel.com', 'secret')->post(...);
```

#### Bearer Tokens

[](#bearer-tokens)

If you would like to quickly add a bearer token to the request's `Authorization` header, you may use the `withToken` method:

```
$response = Http::withToken('token')->post(...);
```

### Timeout

[](#timeout)

The `timeout` method may be used to specify the maximum number of seconds to wait for a response:

```
$response = Http::timeout(3)->get(...);
```

If the given timeout is exceeded, an instance of `Gokure\Http\Client\ConnectionException` will be thrown.

### Retries

[](#retries)

If you would like HTTP client to automatically retry the request if a client or server error occurs, you may use the `retry` method. The `retry` method accepts two arguments: the maximum number of times the request should be attempted, and the number of milliseconds that Hyperf should wait in between attempts:

```
$response = Http::retry(3, 100)->post(...);
```

If all of the requests fail, an instance of `Gokure\Http\Client\RequestException` will be thrown.

### Error Handling

[](#error-handling)

Unlike Guzzle's default behavior, Hyperf's HTTP client wrapper does not throw exceptions on client or server errors (`400` and `500` level responses from servers). You may determine if one of these errors was returned using the `successful`, `clientError`, or `serverError` methods:

```
// Determine if the status code is >= 200 and < 300...
$response->successful();

// Determine if the status code is >= 400...
$response->failed();

// Determine if the response has a 400 level status code...
$response->clientError();

// Determine if the response has a 500 level status code...
$response->serverError();
```

#### Throwing Exceptions

[](#throwing-exceptions)

If you have a response instance and would like to throw an instance of `Gokure\Http\Client\RequestException` if the response status code indicates a client or server error, you may use the `throw` method:

```
$response = Http::post(...);

// Throw an exception if a client or server error occurred...
$response->throw();

return $response['user']['id'];
```

The `Gokure\Http\Client\RequestException` instance has a public `$response` property which will allow you to inspect the returned response.

The `throw` method returns the response instance if no error occurred, allowing you to chain other operations onto the `throw` method:

```
return Http::post(...)->throw()->json();
```

If you would like to perform some additional logic before the exception is thrown, you may pass a closure to the `throw` method. The exception will be thrown automatically after the closure is invoked, so you do not need to re-throw the exception from within the closure:

```
return Http::post(...)->throw(function ($response, $e) {
    //
})->json();
```

### Guzzle Options

[](#guzzle-options)

You may specify additional [Guzzle request options](http://docs.guzzlephp.org/en/stable/request-options.html) using the `withOptions` method. The `withOptions` method accepts an array of key / value pairs:

```
$response = Http::withOptions([
    'debug' => true,
])->get('http://example.com/users');
```

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

[](#concurrent-requests)

Sometimes, you may wish to make multiple HTTP requests concurrently. In other words, you want several requests to be dispatched at the same time instead of issuing the requests sequentially. This can lead to substantial performance improvements when interacting with slow HTTP APIs.

Thankfully, you may accomplish this using the `pool` method. The `pool` method accepts a closure which receives an `Gokure\Http\Client\Pool` instance, allowing you to easily add requests to the request pool for dispatching:

```
use Gokure\Http\Client\Pool;
use Gokure\Http\Client\Http;

$responses = Http::pool(fn (Pool $pool) => [
    $pool->get('http://localhost/first'),
    $pool->get('http://localhost/second'),
    $pool->get('http://localhost/third'),
]);

return $responses[0]->ok() &&
       $responses[1]->ok() &&
       $responses[2]->ok();
```

As you can see, each response instance can be accessed based on the order it was added to the pool. If you wish, you can name the requests using the `as` method, which allows you to access the corresponding responses by name:

```
use Gokure\Http\Client\Pool;
use Gokure\Http\Client\Http;

$responses = Http::pool(fn (Pool $pool) => [
    $pool->as('first')->get('http://localhost/first'),
    $pool->as('second')->get('http://localhost/second'),
    $pool->as('third')->get('http://localhost/third'),
]);

return $responses['first']->ok();
```

Testing
-------

[](#testing)

Many Hyperf services provide functionality to help you easily and expressively write tests, and Hyperf's HTTP wrapper is no exception. The `Http` facade's `fake` method allows you to instruct the HTTP client to return stubbed / dummy responses when requests are made.

### Faking Responses

[](#faking-responses)

For example, to instruct the HTTP client to return empty, `200` status code responses for every request, you may call the `fake` method with no arguments:

```
use Gokure\Http\Client\Http;

Http::fake();

$response = Http::post(...);
```

> {note} When faking requests, HTTP client middleware are not executed. You should define expectations for faked responses as if these middleware have run correctly.

#### Faking Specific URLs

[](#faking-specific-urls)

Alternatively, you may pass an array to the `fake` method. The array's keys should represent URL patterns that you wish to fake and their associated responses. The `*` character may be used as a wildcard character. Any requests made to URLs that have not been faked will actually be executed. You may use the `Http` facade's `response` method to construct stub / fake responses for these endpoints:

```
Http::fake([
    // Stub a JSON response for GitHub endpoints...
    'github.com/*' => Http::response(['foo' => 'bar'], 200, $headers),

    // Stub a string response for Google endpoints...
    'google.com/*' => Http::response('Hello World', 200, $headers),
]);
```

If you would like to specify a fallback URL pattern that will stub all unmatched URLs, you may use a single `*` character:

```
Http::fake([
    // Stub a JSON response for GitHub endpoints...
    'github.com/*' => Http::response(['foo' => 'bar'], 200, ['Headers']),

    // Stub a string response for all other endpoints...
    '*' => Http::response('Hello World', 200, ['Headers']),
]);
```

#### Faking Response Sequences

[](#faking-response-sequences)

Sometimes you may need to specify that a single URL should return a series of fake responses in a specific order. You may accomplish this using the `Http::sequence` method to build the responses:

```
Http::fake([
    // Stub a series of responses for GitHub endpoints...
    'github.com/*' => Http::sequence()
                            ->push('Hello World', 200)
                            ->push(['foo' => 'bar'], 200)
                            ->pushStatus(404),
]);
```

When all of the responses in a response sequence have been consumed, any further requests will cause the response sequence to throw an exception. If you would like to specify a default response that should be returned when a sequence is empty, you may use the `whenEmpty` method:

```
Http::fake([
    // Stub a series of responses for GitHub endpoints...
    'github.com/*' => Http::sequence()
                            ->push('Hello World', 200)
                            ->push(['foo' => 'bar'], 200)
                            ->whenEmpty(Http::response()),
]);
```

If you would like to fake a sequence of responses but do not need to specify a specific URL pattern that should be faked, you may use the `Http::fakeSequence` method:

```
Http::fakeSequence()
        ->push('Hello World', 200)
        ->whenEmpty(Http::response());
```

#### Fake Callback

[](#fake-callback)

If you require more complicated logic to determine what responses to return for certain endpoints, you may pass a closure to the `fake` method. This closure will receive an instance of `Gokure\Http\Client\Request` and should return a response instance. Within your closure, you may perform whatever logic is necessary to determine what type of response to return:

```
Http::fake(function ($request) {
    return Http::response('Hello World', 200);
});
```

### Inspecting Requests

[](#inspecting-requests)

When faking responses, you may occasionally wish to inspect the requests the client receives in order to make sure your application is sending the correct data or headers. You may accomplish this by calling the `Http::assertSent` method after calling `Http::fake`.

The `assertSent` method accepts a closure which will receive an `Gokure\Http\Client\Request` instance and should return a boolean value indicating if the request matches your expectations. In order for the test to pass, at least one request must have been issued matching the given expectations:

```
use Gokure\Http\Client\Request;
use Gokure\Http\Client\Http;

Http::fake();

Http::withHeaders([
    'X-First' => 'foo',
])->post('http://example.com/users', [
    'name' => 'Taylor',
    'role' => 'Developer',
]);

Http::assertSent(function (Request $request) {
    return $request->hasHeader('X-First', 'foo') &&
           $request->url() == 'http://example.com/users' &&
           $request['name'] == 'Taylor' &&
           $request['role'] == 'Developer';
});
```

If needed, you may assert that a specific request was not sent using the `assertNotSent` method:

```
use Gokure\Http\Client\Request;
use Gokure\Http\Client\Http;

Http::fake();

Http::post('http://example.com/users', [
    'name' => 'Taylor',
    'role' => 'Developer',
]);

Http::assertNotSent(function (Request $request) {
    return $request->url() === 'http://example.com/posts';
});
```

Or, you may use the `assertNothingSent` method to assert that no requests were sent during the test:

```
Http::fake();

Http::assertNothingSent();
```

License
-------

[](#license)

Released under the MIT License, see [LICENSE](LICENSE).

###  Health Score

28

—

LowBetter than 51% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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

Every ~32 days

Total

7

Last Release

1731d ago

### Community

Maintainers

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

---

Top Contributors

[![gokure](https://avatars.githubusercontent.com/u/88591?v=4)](https://github.com/gokure "gokure (19 commits)")

---

Tags

httphttp clientGuzzlehyperf

### Embed Badge

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

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

###  Alternatives

[psr/http-client

Common interface for HTTP clients

1.7k750.0M3.5k](/packages/psr-http-client)[e-moe/guzzle6-bundle

Integrates Guzzle 6 into your Symfony application

11263.3k1](/packages/e-moe-guzzle6-bundle)[amphp/http-client-guzzle-adapter

Guzzle adapter for Amp's HTTP client.

1545.9k3](/packages/amphp-http-client-guzzle-adapter)[openapi/openapi-sdk

Minimal and agnostic PHP SDK for Openapi® (https://openapi.com)

164.6k1](/packages/openapi-openapi-sdk)[opgg/riotquest

RiotQuest, PHP RiotAPI client library that focused on multi request from OP.GG

172.6k](/packages/opgg-riotquest)

PHPackages © 2026

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