PHPackages                             abollinger/response - 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. [API Development](/categories/api)
4. /
5. abollinger/response

ActiveLibrary[API Development](/categories/api)

abollinger/response
===================

PHP Api response management for the partez framework.

v1.0.7(2mo ago)0981MITPHP

Since May 14Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Antoine-Bollinger/response)[ Packagist](https://packagist.org/packages/abollinger/response)[ RSS](/packages/abollinger-response/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (2)Versions (9)Used By (1)

Abollinger\\Response
====================

[](#abollingerresponse)

**Abollinger\\Response** is a lightweight PHP utility class that provides a consistent and structured way to format and send API responses in JSON. It simplifies the process of setting HTTP status codes, success flags, messages, and response data — all while supporting CORS headers and clean output handling.

Features
--------

[](#features)

- **Consistent API Responses**: Set and retrieve standard response fields including `code`, `success`, `message`, `state`, and `data`.
- **Fluent Interface**: Chain methods for clean and expressive response configuration.
- **CORS Headers**: Automatically includes standard CORS headers in every JSON response.
- **Safe Output**: Proper content type and character encoding headers set for JSON APIs.
- **Reset Capability**: Easily reset response to predefined defaults.

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- Composer autoloading (for dependency resolution, e.g., `Helpers::defaultParams()`)

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

[](#installation)

Include the `Response` class via Composer autoloading. Ensure you have the required `Helpers` class available in your project.

```
composer require abollinger/helpers
```

Then, load the class in your code:

```
require_once 'vendor/autoload.php';

use Abollinger\Response;
```

Usage
-----

[](#usage)

### Initialization

[](#initialization)

```
$response = new Response();
```

### Set and Send a Basic Response

[](#set-and-send-a-basic-response)

```
$response
    ->setCode(200)
    ->setSuccess(true)
    ->setMessage("Data fetched successfully.")
    ->setData(['item1', 'item2', 'item3'])
    ->sendJSON();
```

### Use Fluent Method Chaining

[](#use-fluent-method-chaining)

```
$response
    ->set([
        'code' => 201,
        'message' => 'Created successfully.',
        'data' => ['id' => 42]
    ])
    ->sendJSON();
```

### Reset to Defaults

[](#reset-to-defaults)

```
$response->reset();
```

API Reference
-------------

[](#api-reference)

### Constructor

[](#constructor)

`__construct(array $default = [])`

- Initializes the response with default parameters.
- You can override defaults by passing a custom array.

### Methods

[](#methods)

#### `sendJSON(bool $terminate = true): void`

[](#sendjsonbool-terminate--true-void)

Sends the current response as JSON with appropriate headers and optional script termination.

#### `toJson(int $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES): string`

[](#tojsonint-options--json_unescaped_unicode--json_unescaped_slashes-string)

Returns the JSON representation of the current response.

#### `set(array $params = []): self`

[](#setarray-params---self)

Merges provided parameters into the current response array.

#### `setCode(int $code): self`

[](#setcodeint-code-self)

Sets the HTTP response code.

#### `setSuccess(bool $success): self`

[](#setsuccessbool-success-self)

Sets the success flag.

#### `setMessage(string $message): self`

[](#setmessagestring-message-self)

Sets the human-readable message.

#### `setState(int $state): self`

[](#setstateint-state-self)

Sets a numeric state identifier.

#### `setData(array|string $data): self`

[](#setdataarraystring-data-self)

Sets the payload data of the response.

#### `get(): array`

[](#get-array)

Returns the entire response array.

#### `getCode(): int`

[](#getcode-int)

Returns the current response code.

#### `getSuccess(): bool`

[](#getsuccess-bool)

Returns the success status.

#### `getMessage(): string`

[](#getmessage-string)

Returns the response message.

#### `getState(): int`

[](#getstate-int)

Returns the state code.

#### `getData(): array|string`

[](#getdata-arraystring)

Returns the response data.

#### `reset(): array`

[](#reset-array)

Resets the response to its default parameters.

Example Workflow
----------------

[](#example-workflow)

```
use Abollinger\Response;

$response = new Response();

$response
    ->setCode(200)
    ->setSuccess(true)
    ->setMessage("User logged in successfully.")
    ->setData(['token' => 'abc123'])
    ->sendJSON();
```

License
-------

[](#license)

This library is licensed under the MIT License. For full license details, see the `LICENSE` file distributed with this source code.

Author
------

[](#author)

Antoine Bollinger
Email:

For contributions, issues, or feedback, feel free to reach out or open an issue on the relevant repository.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance87

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

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

Recently: every ~0 days

Total

8

Last Release

62d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4ef76077f29bfc3b32b0b1d21d2f4122af8109f23aa48586ea68d768e01f354f?d=identicon)[antoinebollinger](/maintainers/antoinebollinger)

---

Top Contributors

[![antoinebollinger](https://avatars.githubusercontent.com/u/56133015?v=4)](https://github.com/antoinebollinger "antoinebollinger (9 commits)")

### Embed Badge

![Health badge](/badges/abollinger-response/health.svg)

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

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

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