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

ActiveLibrary[API Development](/categories/api)

blacknell/restapi-service
=========================

Simple class to expose a REST api

0.7(5y ago)0242[1 PRs](https://github.com/blacknell/restapi-service/pulls)MITPHP

Since Jan 13Pushed 1y ago1 watchersCompare

[ Source](https://github.com/blacknell/restapi-service)[ Packagist](https://packagist.org/packages/blacknell/restapi-service)[ RSS](/packages/blacknell-restapi-service/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (5)Versions (8)Used By (0)

A simple class to expose a RESTful api
======================================

[](#a-simple-class-to-expose-a-restful-api)

[![Build Status](https://camo.githubusercontent.com/9beb3930d61b8b2c20a61bc16999438ccd159c27c907be0365cf021d74f21678/68747470733a2f2f7472617669732d63692e6f72672f626c61636b6e656c6c2f726573746170692d736572766963652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/blacknell/restapi-service)[![Latest Stable Version](https://camo.githubusercontent.com/4e6c4b4f2251621942508be2e20f6c2b6a10fd6df903b47a8f01adc3dab80172/68747470733a2f2f706f7365722e707567782e6f72672f626c61636b6e656c6c2f726573746170692d736572766963652f762f737461626c65)](https://packagist.org/packages/blacknell/restapi-service)[![Latest Unstable Version](https://camo.githubusercontent.com/b8b338e5876b772f1ce4bd48a3bca9c6ff7db0cb63e017898e723a6ec08c1206/68747470733a2f2f706f7365722e707567782e6f72672f626c61636b6e656c6c2f726573746170692d736572766963652f762f756e737461626c65)](https://packagist.org/packages/blacknell/restapi-service)[![License](https://camo.githubusercontent.com/0249def3b79f91f04a4f4727e49677376b5481875668c17aa2efa76d90f7382b/68747470733a2f2f706f7365722e707567782e6f72672f626c61636b6e656c6c2f726573746170692d736572766963652f6c6963656e7365)](https://packagist.org/packages/blacknell/restapi-service)

`restapi-service` maps REST API calls to endpoints in protected methods in your derived class. In your class methods process according to the verbs and arguments of the http request.

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

[](#installation)

Install the latest version with

```
$ composer require blacknell/restapi-service

```

Basic Usage
-----------

[](#basic-usage)

- Copy `example/api.php` and derive a class such as in `examples/MyAPI.class.php` into your web server directory
- Configure a `.htaccess` file to rewrite your RESTful call to your class

Web Server configuration
------------------------

[](#web-server-configuration)

For example, `https://yourserver/myapi/v1/daylight/littlehampton/yesterday` maps to `https://yourserver/myapi/v1/api.php?request=daylight/littlehampton/yesterday`

```

    RewriteEngine On
    RewriteRule myapi/v1/(.*)$ myapi/v1/api.php?request=$1 [QSA,NC,L]

```

Sample code
-----------

[](#sample-code)

See [example/MyAPI.class.php](https://github.com/blacknell/restapi-service/blob/master/example/MyAPI.class.php)to see how `https://yourserver/myapi/v1/daylight/littlehampton/yesterday` generates the following JSON output

```
{
    "description": "Between sunrise and sunset yesterday",
    "sunrise": {
        "date": "2019-01-07 08:00:56.000000",
        "timezone_type": 3,
        "timezone": "Europe\/London"
    },
    "sunset": {
        "date": "2019-01-07 16:15:44.000000",
        "timezone_type": 3,
        "timezone": "Europe\/London"
    }
}

```

Additional Concepts
-------------------

[](#additional-concepts)

#### Cross-Origin Resource Sharing (CORS)

[](#cross-origin-resource-sharing-cors)

Additional headers can be added to the constructor of your derived class **before** calling the parent constructor. For example, to allow a client on a website  to access your API add this header call.

```
	public function __construct($request, \Monolog\Logger $logger = null)
	{
		header('Access-Control-Allow-Origin: http://myclient.com');
		parent::__construct($request, $logger);
	}

```

#### Authentication

[](#authentication)

Overide `RestAPI::isAuthenticated()` to handle authentication and only return `true` if the request is authorised. As a basic example, you could enforce a request to include a header such as `Authentication-Token: xxx` and test this in your derived class.

```
	protected function isAuthenticated()
	{
		$headers=getallheaders();
		if($headers['Authentication-Token'] !== 'xxx') {
			return false;
		} else {
			return parent::isAuthenticated();
		}
	}

```

Error Handling
--------------

[](#error-handling)

Any endpoint not mapping to a protected function in your derived class results in the following JSON response.

```
{
    "error": "No endpoint",
    "code": 404
}

```

Your derived class should do the same for invalid verbs or arguments. Methods other than GET, POST, PUT or DELETE also result in an error.

Logging
-------

[](#logging)

PSR-3 logging is supported via [monolog/monolog](https://github.com/Seldaek/monolog) by passing an optional `Logger` object to the API constructor.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

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

Recently: every ~127 days

Total

6

Last Release

1928d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/42e42d077b60ac57c4a4c4c12023a2b6228370f6de5ebd650e819724d91d1f7b?d=identicon)[blacknell](/maintainers/blacknell)

---

Top Contributors

[![blacknell](https://avatars.githubusercontent.com/u/8863732?v=4)](https://github.com/blacknell "blacknell (60 commits)")

---

Tags

phprest-api

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/blacknell-restapi-service/health.svg)

```
[![Health](https://phpackages.com/badges/blacknell-restapi-service/health.svg)](https://phpackages.com/packages/blacknell-restapi-service)
```

###  Alternatives

[googleads/googleads-php-lib

Google Ad Manager SOAP API Client Library for PHP

67410.3M25](/packages/googleads-googleads-php-lib)[googleads/google-ads-php

Google Ads API client for PHP

3497.6M9](/packages/googleads-google-ads-php)[team-reflex/discord-php

An unofficial API to interact with the voice and text service Discord.

1.1k379.4k24](/packages/team-reflex-discord-php)[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[checkout/checkout-sdk-php

Checkout.com SDK for PHP

553.3M7](/packages/checkout-checkout-sdk-php)[convertkit/convertkitapi

Kit PHP SDK for the Kit API

2167.1k1](/packages/convertkit-convertkitapi)

PHPackages © 2026

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