PHPackages                             slexphp/simple-http-api-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. slexphp/simple-http-api-client

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

slexphp/simple-http-api-client
==============================

A simple PSR compatible JSON HTTP API client

v3.0.0(4y ago)010.6k—0%MITPHPPHP &gt;=8.0

Since Feb 11Pushed 4y ago1 watchersCompare

[ Source](https://github.com/slepic/slexphp-simple-http-api-client)[ Packagist](https://packagist.org/packages/slexphp/simple-http-api-client)[ Docs](https://github.com/slepic/slexphp-simple-http-api-client)[ RSS](/packages/slexphp-simple-http-api-client/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (6)Versions (4)Used By (0)

About
=====

[](#about)

A simple PSR compatible PHP HTTP API client

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

[](#installation)

Install via [Composer](https://getcomposer.org/) by running `composer require slexphp/simple-http-api-client` in your project directory.

Public API
----------

[](#public-api)

### Classes

[](#classes)

#### `Slexphp\Http\SimpleApiClient\Psr\JsonClientFactory\JsonApiClient`

[](#slexphphttpsimpleapiclientpsrjsonclientfactoryjsonapiclient)

This class provides a static `create()` method.

It accepts a psr client and request factory and creates an instance of `ApiClientInterface` (see below)..

The `create()` method with it's first two arguments (plus return type and thrown exception interfaces), are the only part of public API of this library.

The actual returned class as well as any details behind it should not be relied upon at this point.

```
$jsonClient = JsonApiClient::create($psrRequestFactory, $psrClient);

```

### Interfaces

[](#interfaces)

#### `Slexphp\Http\SimpleApiClient\Contracts\ApiClientInterface`

[](#slexphphttpsimpleapiclientcontractsapiclientinterface)

This is the main interface with which all your api clients will interact. It offers just one method with all parameters required to send a http request.

```
$client->call($baseUrl, $method, $endpoint, $query, $headers, $body);

```

It will also process request body serialization and response body deserialization according to passed and received content-type headers.

Implementation should by default fallback to application/json content type request body serializer if no request content-type header is provided. Implementation should also by default fallback to application/json content type response body deserializer if no response content-type header is received from the server.

Nevertheless, passsing request content-type explicitly in request headers is recommended. Because using a default request body serializer doesn't imply adding default content-type header to the actual request.

#### `Slexphp\Http\SimpleApiClient\Contracts\ApiResponseInterface`

[](#slexphphttpsimpleapiclientcontractsapiresponseinterface)

This interface describes the api response object. It resembles PSR-7 `\Psr\Http\Message\ResponseInterface`, but it has no mutator methods and it offers `getParsedBody()` method so that you dont need to worry about parsing.

#### `Slexphp\Http\SimpleApiClient\Contracts\ApiClientExceptionInterface`

[](#slexphphttpsimpleapiclientcontractsapiclientexceptioninterface)

This interface describes all possible errors that can occur during a http call or during (de)serialization of request/response messages.

### Example

[](#example)

In this example we already created a client instance and we make a single http call. Then we show all the methods of the response/error api while we make all possible assertions that are always going to be true.

```
try {
    $response = $jsonClient->call(
        'https://myapi.com',
        'POST',
        '/some/endpoint',
        ['queryParam' => 'value'], // query
        ['Authorization' => 'Basic u:p'], // headers
        ['bodyProperty' => 'value'] // body
    );
} catch (ApiClientExceptionInterface $e) {
    $code = $e->getCode();
    var_dump($code);

    $response = $e->getResponse();
    if (!$response) {
        // connect error
        assert($code === 0);
        throw $e;
    }

    $status = $response->getStatusCode();
    var_dump($status);
    assert($code === $status);
    var_dump($response->getHeaders());

    if ($response->getRawBody()) {
        var_dump($response->getRawBody());
        assert($response->getRawBody() !== '');
        if ($response->getParsedBody() === null) {
            // response body parse error, any http status code
        } else {
            // non 2xx response
            assert($status < 200 || $status >= 300);
            var_dump($response->getParsedBody());
            assert(\is_array($response->getParsedBody());
        }
    } else {
        // no response body, only non 2xx responses
        assert($status < 200 || $status >= 300);
        assert($response->getRawBody() === '');
        assert($response->getParsedBody() === null);
    }

    throw $e;
}

// 2xx responses succesfully parsed

$status = $response->getStatusCode();
var_dump($status);
assert($status >= 200 && $status < 300);
var_dump($response->getHeaders());

if ($response->getRawBody()) {
    var_dump($response->getRawBody());
    assert($response->getRawBody() !== '');
    var_dump($response->getParsedBody());
    assert(\is_array($response->getParsedBody());
} else {
    assert($response->getRawBody() === '');
    assert($response->getParsedBody() === null);
}

```

The ApiClientInterface implementation is a stateless service (as long as the underlying response factory and client are). You can use just one instance for all your backend-backend calls.

###  Health Score

31

—

LowBetter than 71% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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 ~129 days

Total

3

Last Release

1605d ago

Major Versions

v1.0.0 → v2.0.02021-10-21

v2.0.0 → v3.0.02021-10-29

PHP version history (2 changes)v1.0.0PHP &gt;=7.4

v3.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5dd1cd12d4fe4974ad1e3609065ac4eb13c29023606c289c08153c432da827cf?d=identicon)[slepic](/maintainers/slepic)

---

Top Contributors

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

---

Tags

httppsrjsonapiclientrest

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/slexphp-simple-http-api-client/health.svg)

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

###  Alternatives

[vinelab/http

An http library developed for the laravel framework. aliases itself as HttpClient

57299.6k10](/packages/vinelab-http)[jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

2225.8k1](/packages/jsor-hal-client)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69110.0k](/packages/serpapi-google-search-results-php)[ismaeltoe/osms

PHP library wrapper of the Orange SMS API.

4639.8k](/packages/ismaeltoe-osms)[rap2hpoutre/jacky

Opinionated REST JSON HTTP API client for laravel

174.3k](/packages/rap2hpoutre-jacky)

PHPackages © 2026

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