PHPackages                             gajosu/laravel-http-service - 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. gajosu/laravel-http-service

ActiveLibrary

gajosu/laravel-http-service
===========================

This is my package laravel-http-service

v1.0.3(3y ago)01.1k↓100%[2 PRs](https://github.com/gajosu/laravel-http-service/pulls)MITPHPPHP ^8.1

Since Jul 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/gajosu/laravel-http-service)[ Packagist](https://packagist.org/packages/gajosu/laravel-http-service)[ Docs](https://github.com/gajosu/laravel-http-service)[ RSS](/packages/gajosu-laravel-http-service/feed)WikiDiscussions main Synced today

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

Laravel Http Client Service
===========================

[](#laravel-http-client-service)

[![Latest Version on Packagist](https://camo.githubusercontent.com/8f0ed82536756f829ecdbafe3ab89be70ff6c7c6b9e566938a6121c5132879fa/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67616a6f73752f6c61726176656c2d687474702d736572766963652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gajosu/laravel-http-service)[![GitHub Tests Action Status](https://camo.githubusercontent.com/c1837a2ff7219bd2317abad1a5f08c0e07442d9b5a37a3cad05694a77c9f6c28/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f67616a6f73752f6c61726176656c2d687474702d736572766963652f72756e2d74657374733f6c6162656c3d7465737473)](https://github.com/gajosu/laravel-http-service/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/e952392cd85a45d62e04431eb372eb386a4453e5d7519cfa2134ad5868d5049f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f67616a6f73752f6c61726176656c2d687474702d736572766963652f436865636b253230262532306669782532307374796c696e673f6c6162656c3d636f64652532307374796c65)](https://github.com/gajosu/laravel-http-service/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/7fb64fb7f48b0e3d6d66904026b9bb8f7a854af51c480bf5c9d80bd232c01244/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67616a6f73752f6c61726176656c2d687474702d736572766963652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/gajosu/laravel-http-service)

Do you make a lot of http requests to microservices or external apis in your laravel projects?

There are many packages to do this, but I haven't found one that is easy to test and that offers caching functionality.
This package allows you to create your own services, make requests and test them easily.

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

[](#installation)

You can install the package via composer:

```
composer require gajosu/laravel-http-service
```

Usage
-----

[](#usage)

You can start using it easily, simply by calling the `request()` method of the facade `HttpService`

```
use Gajosu\LaravelHttpClient\Facades\HttpService;

/** @var \Gajosu\LaravelHttpClient\Request\ApiRequestBuilder $builder */
$builder = HttpService::request()
    ->setMethod('POST')
    //set base url
    ->setBaseUri('http://example.com')
    //set path
    ->setPath('/test')
    // set headers
    ->setHeaders([
        'Authorization' => 'Basic {YOUR_TOKEN}'
    ])
    //set queries strings
    ->setQuery([
        'query1' => 'param'
    ])
    //set body
    ->setBody([
        'field1' => 'value'
    ]);
// send the request and get response
/** @var \Gajosu\LaravelHttpClient\Response\ApiResponse $response */
$response = $builder->send();
// json data decoded
$data = $response->json();
```

Make your own service
---------------------

[](#make-your-own-service)

As explained in the example above, you can start using the package with its base classes, but the purpose of this package is to make it easy to create your own services.

You can make your own service by extending the `Gajosu\LaravelHttpClient\HttpService` class.
Rewrite the `getBuilder()` method and set Base Uri, headers, queries, etc.

In the next example we will make a service that sends requests to `http://myservice.com` base url and set a access token in the headers.

```
namespace App\Services\MyService;

use Gajosu\LaravelHttpClient\HttpService;
use Gajosu\LaravelHttpClient\Contracts\HttpRequestBuilder;

class MyService extends HttpService
{
    private ?string $access_token = null;

    public function setAccessToken(string $access_token): void
    {
        $this->access_token = $access_token;
    }

    public function getAccessToken(): ?string
    {
        return $this->access_token;
    }

    public function getBuilder(): HttpRequestBuilder
    {
        return parent::getBuilder()
            ->setBaseUri('http://myservice.com')
            ->setHeaders([
                "Authorization" => "Basic {$this->access_token}"
            ]);
    }
}
```

The next step is to create a facade for the service extending the `Gajosu\LaravelHttpClient\Facades\HttpService` class.

```
namespace App\Services\MyService\Facades\MyService;

use Gajosu\LaravelHttpClient\Facades\HttpService;

/**
 * @method static void setAccessToken(string $access_token)
 * @method static string getAccessToken()
 */
class MyService extends HttpService
{

    protected static function getFacadeAccessor()
    {
        return \App\Services\MyService\MyService::class;
    }
}
```

Now you can use the service in your application.

```
use App\Services\MyService\Facades\MyService;

MyService::setAccessToken('{YOUR_TOKEN}');
$response = MyService::request()
    ->setMethod('POST')
    ->setPath('/test')
    ->setQuery([
        'query1' => 'param'
    ])
    ->setBody([
        'field1' => 'value'
    ])
    ->send();
$data = $response->json();
```

Caching Responses
-----------------

[](#caching-responses)

The package also allows you to cache the responses.

```
use App\Services\MyService\Facades\MyService;

MyService::setAccessToken('{YOUR_TOKEN}');
$response = MyService::request()
    ->setMethod('GET')
    ->setPath('/test')
    ->setQuery([
        'query1' => 'param'
    ])
    //you can set the cache time in seconds
    ->cacheFor(60)
    // or
    // ->cacheFor(now()->addMinutes(1))
    // you can also keep the cache forever
    // ->cacheForever()
    ->send();
$data = $response->json();
```

Faking Responses
----------------

[](#faking-responses)

For example, to instruct the package to return a fake response with code 200 and empty body for all requests, you can use the `fake()` method.

```
use App\Services\MyService\Facades\MyService;

MyService::fake()
$response = MyService::request()
    ->setMethod('GET')
    ->setPath('/test')
    ->setQuery([
        'query1' => 'param'
    ])
    ->fakeResponse(200, [])
    ->send();
$data = $response->json();
```

### Specifying responses

[](#specifying-responses)

You can also specify the responses code, headers and body passing an array to the `shouldReceiveResponses` method.

```
use App\Services\MyService\Facades\MyService;

MyService::fake();
MyService::shouldReceiveResponses([
    [
        new \GuzzleHttp\Psr7\Response(
            status : 200,
            body: '{"success" : true}'
        ),

        new \GuzzleHttp\Psr7\Response(
            status : 201,
            body: '{"created" : true}'
        )
    ]
]);

$response = MyService::request()
    ->setMethod('GET')
    ->setPath('/test')
    ->setQuery([
        'query1' => 'param'
    ])
    ->send();

//get first fake response
// [
//   "success" => true
// ]
$data = $response->json();

//get second fake response
// [
//   "created" => true
// ]
$data = $response->json();
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/gajosu/laravel-http-service/blob/main/.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Gabriel Gonzalez](https://github.com/gajosu)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 63% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 76.6% 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 ~4 days

Total

4

Last Release

1333d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6ba7af87881476b7e21c68b907b3b0615a3213575e3dc896ac42cfebde210e81?d=identicon)[gajosu](/maintainers/gajosu)

---

Top Contributors

[![gajosu](https://avatars.githubusercontent.com/u/7597189?v=4)](https://github.com/gajosu "gajosu (49 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (7 commits)")

---

Tags

http-clientlaravelmicroservicelaravelgajosularavel-http-service

###  Code Quality

TestsPest

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/gajosu-laravel-http-service/health.svg)

```
[![Health](https://phpackages.com/badges/gajosu-laravel-http-service/health.svg)](https://phpackages.com/packages/gajosu-laravel-http-service)
```

###  Alternatives

[ralphjsmit/laravel-helpers

A package containing handy helpers for your Laravel-application.

13662.4k1](/packages/ralphjsmit-laravel-helpers)[musahmusah/laravel-multipayment-gateways

A Laravel Package that makes implementation of multiple payment Gateways endpoints and webhooks seamless

852.2k1](/packages/musahmusah-laravel-multipayment-gateways)[steadfast-courier/steadfast-courier-laravel-package

A complate Laravel package for SteadFast Courier Limited

142.4k](/packages/steadfast-courier-steadfast-courier-laravel-package)

PHPackages © 2026

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