PHPackages                             johnrdorazio/simpleapi - 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. johnrdorazio/simpleapi

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

johnrdorazio/simpleapi
======================

jumpstart your API (whether RESTful or not) with a base class that you can fine tune for any implementation

v0.1.3(3y ago)011[1 issues](https://github.com/JohnRDOrazio/SimpleAPI/issues)Apache-2.0PHPPHP &gt;=7.4.0

Since Jan 19Pushed 3y ago1 watchersCompare

[ Source](https://github.com/JohnRDOrazio/SimpleAPI)[ Packagist](https://packagist.org/packages/johnrdorazio/simpleapi)[ Docs](https://github.com/JohnRDOrazio/SimpleAPI)[ RSS](/packages/johnrdorazio-simpleapi/feed)WikiDiscussions main Synced today

READMEChangelog (3)DependenciesVersions (5)Used By (0)

SimpleAPI
=========

[](#simpleapi)

[![CodeFactor](https://camo.githubusercontent.com/fd74a546b407135d230aa7f98bcb0a1823ffb04ceb20d34585d556ae189deb80/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f6a6f686e72646f72617a696f2f73696d706c656170692f6261646765)](https://www.codefactor.io/repository/github/johnrdorazio/simpleapi)

Jumpstart your API (whether RESTful or not) and fine tune it for your own API implementation.

There are so many discussions about what is truly RESTful and what is not, this project doesn't pretend to be truly RESTful out of the box.

The **SimpleAPI** simply aims to remove some of the hassle of the initial setup of an API in PHP, without depending on any frameworks.

It aims to be extensible so as to cater to various use cases, and to various kinds of APIs. In building your own API, you can decide whether it should be fully RESTful or not, based on what you are trying to achieve.

What the SimpleAPI is NOT
-------------------------

[](#what-the-simpleapi-is-not)

The **SimpleAPI** does not create or handle routes (for now at least). This should be handled by your own API implementation.

The **SimpleAPI** does not handle internationalization or localization of your response: this should be handled by your own API implementation, an example of which is provided in the **SampleAPI** class (`SampleAPI.php`).

The **SimpleAPI** does not create any kind of client implementation of the API. It simply helps you jumpstart your API, to handle requests from clients and produce responses. Client implementations should be created separately.

What the SimpleAPI IS
---------------------

[](#what-the-simpleapi-is)

The **SimpleAPI** simply handles detection of `Request` methods and `Accept` headers, and takes care of preparing the `Response` accordingly, with the correct `Response` headers and `Content type`.

It will assist you in defining the accepted parameters that the `Request` can or should include, in sanitizing the values of `Request` parameters based on the type you have defined for each parameter, and in generating the correct `Response` based on those parameters. It will generate `Response` headers for both valid and invalid requests, so as to assist the requesting party in making the correct `Request` to your API.

The **SimpleAPI** offers a sample implementation in the `SampleAPI` class, which can assist you in defining your own API implementation. Go ahead and adapt the `SampleAPI` class to fit your own API implementation.

The `SampleAPI` class is not an API endpoint, it merely determines the behaviour of your API. The `SampleAPI` (or any API implementation you may create) should define configurations for the `SimpleAPI` package with a `config.php` based on `config.sample.php`.

An example endpoint is included as the file `endpoint.php`: this simply instantiates the `SampleAPI` class, which in turn instantiates the `SimpleAPI` class using the configurations in `config.sample.php`.

Why the SimpleAPI ?
-------------------

[](#why-the-simpleapi-)

Many API implementations nowadays are handled with [Ruby on Rails](https://rubyonrails.org/), or [Python](https://www.python.org/) + [Flask](https://flask.palletsprojects.com/), or with complex frameworks such as [Laravel](https://laravel.com/) that have a lot of functionality.

However sometimes you may need to just create a simple API service without a lot of overhead, and without needing to learn a whole new language or the complexities of a framework.

And perhaps you are familiar with [PHP](https://www.php.net/) and would like to stick to a simple PHP implementation.

The **SimpleAPI** aims to jumpstart your simple API implementation, removing the initial overhead of defining the basics of handling request headers and response headers.

The **SimpleAPI** is compliant with the HTTP2 protocol.

Minimum PHP version
-------------------

[](#minimum-php-version)

The **SimpleAPI** requires a minimum PHP version of [7.4](https://www.php.net/releases/7_4_0.php), seeing that it makes use of typed properties in order to have consistency throughout the codebase.

The **SimpleAPI** defines faux `enum`s using simple classes, so as not to require PHP 8.1; in the future it may implement true enums requiring a minimum PHP version of 8.1.

Create your API implementation in 3 steps
-----------------------------------------

[](#create-your-api-implementation-in-3-steps)

Here are a few simple steps to get you started in creating your own API:

1. Create your API folder and require the SimpleAPI package

    > *This step takes for granted that you have `composer` already installed in your system.*

    ```
    mkdir MyApi
    cd MyApi
    composer require johnrdorazio/simpleapi
    ```
2. Create your own API definition

    On a Linux system:

    ```
    touch MyApi.php
    touch config.php
    ```

    On a Windows system:

    ```
    copy nul MyApi.php
    copy nul config.php
    ```

    Paste the contents of [SampleAPI.php](SampleAPI.php) into `MyApi.php` and adapt to your needs (changing the class name for starters), or simply write from scratch your own API implementation using the `SimpleAPI` package, defining your API parameters (if any):

    ```
