PHPackages                             jessesoeteman/api-return - 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. jessesoeteman/api-return

Abandoned → [jessesoeteman/api-handler](/?search=jessesoeteman%2Fapi-handler)Library[API Development](/categories/api)

jessesoeteman/api-return
========================

This class can be used to manage the status of an api request.

v1.2.2(3y ago)112MITPHPPHP &gt;=8.0

Since Jan 12Pushed 3y agoCompare

[ Source](https://github.com/JesseSoeteman/APIHandler)[ Packagist](https://packagist.org/packages/jessesoeteman/api-return)[ RSS](/packages/jessesoeteman-api-return/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)DependenciesVersions (7)Used By (0)

APIHandler
==========

[](#apihandler)

This class can be used to manage the status of an api request.

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

[](#requirements)

There are a couple things you will need in order to use this php class.

- php 8.0
- composer

How to use APIHandler?
----------------------

[](#how-to-use-apihandler)

### Require APIHandler

[](#require-apihandler)

If you have composer initialized you can run this command in you terminal to download APIHandler.

```
$ composer require jessesoeteman/api-handler
```

### Require the autoloader and APIHandler

[](#require-the-autoloader-and-apihandler)

You will need to require the autoloader in your code, after that you can 'use' the APIReturn class.

```
require __DIR__ . '/vendor/autoload.php';

use APIHandler\APIHandler;
```

### Basic usage

[](#basic-usage)

#### **Declare the class**

[](#declare-the-class)

To start you will need to initialize the class. The class needs one parameter that tells one of the following request types:

- get\_request
- post\_request

In this example we will use a GET request.

```
$apiHandler = new APIHandler(get_request);
```

If the request type is not right the class will echo and exit:

```
{
    "status": "error",
    "errors": [
        "Request method needs to be ...."
    ]
}
```

#### **Error Handling**

[](#error-handling)

Let's say the an error occured, we can then add the error to the errors array. The first parameter can be a string or an array, this contains the error you want to add. The second parameter is a boolean (false on default), if the boolean is true, the script will echo a json object and exit.

```
$apiHandler->addError("error 1");
$apiHandler->addError("error 2");

$apiHandler->addError("error 3", true); // addError("error 4");
```

```
$apiHandler->addError("error 1");
$apiHandler->addError("error 2");
$apiHandler->addError("error 3");

$apiHandler->APIExitOnError(); // addError("error 4");
$apiHandler->addData([]);
$apiHandler->APIExit();
```

```
$apiHandler->addError("error 1");
$apiHandler->addError("error 2");
$apiHandler->addError("error 3");

$apiHandler->addData([]);

$apiHandler->APIExit(); // addError("error 1"); // Error wont be added.
}
$apiHandler->APIExitOnError(); // addData({});
$apiHandler->addData([]);
$apiHandler->addData("Hello World!");

$apiHandler->APIExit(); //
