Download the PHP package blacknell/restapi-service without Composer
On this page you can find all versions of the php package blacknell/restapi-service. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download blacknell/restapi-service
More information about blacknell/restapi-service
Files in blacknell/restapi-service
Package restapi-service
Short Description Simple class to expose a REST api
License MIT
Informations about the package restapi-service
A simple class to expose a RESTful api
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
Install the latest version with
Basic Usage
- Copy
example/api.php
and derive a class such as inexamples/MyAPI.class.php
into your web server directory - Configure a
.htaccess
file to rewrite your RESTful call to your class
Web Server configuration
For example, https://yourserver/myapi/v1/daylight/littlehampton/yesterday
maps to
https://yourserver/myapi/v1/api.php?request=daylight/littlehampton/yesterday
Sample code
See example/MyAPI.class.php
to see how https://yourserver/myapi/v1/daylight/littlehampton/yesterday
generates the following JSON output
Additional Concepts
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 http://myclient.com to access your API add this header call.
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.
Error Handling
Any endpoint not mapping to a protected function in your derived class results in the following JSON response.
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
PSR-3 logging is supported via monolog/monolog by passing
an optional Logger
object to the API constructor.