PHP REST Services

PHP REST Services

RESTful web service API framework for PHP

Creating REST Services

To create a RESTful API using WiseLoop REST Services the developer must follow some certain steps.
This tutorial will walk through the steps by creating a basic hello API service.

1. Define the service controllers

class sampleHelloRestControllerV1 extends wlRestController {

For more information, please refer to Creating service controllers .

2. Default REST Settings

Default settings are stored in rest-settings.ini file. They should be specified as key/value pairs key=value just like php.ini file does.
A special attention should be paid to the route configuration. The route is used to parse the request URL and to determine the service controller name, version, action and the query parameters.
The default route format is route=controller/version/action.
In this case, after submitting http ://rest-service-url-endpoint/hi/v1/hello/John/Mike, the parsed query array will have: ('controller'=>'hi', 'version'='v1', 'action'=>'hello'). The URL query parameters (John, Mike) are available when calling wlRestRequest::getQueryParams method.


3. A custom settings file (optional)

A custom settings file can be given also. The new settings are merged with the existing settings data, the new keys will overwrite the pre-existing ones. By knowing this, for example here is the place where you can define a new route for the URLs accessing the API service.
Also, this is the recommended spot to place other setting parameters for any other custom triggers, output handlers etc.
A sample of a custom settings file (let's call it custom-rest-settings.ini) could look like this:


In the sample above, the default route was overwritten with the one specified in the custom settings file and now the URL accesing the service should look like http ://rest-service-url-endpoint/AKEY/hi/v1/hello/John/Mike. The parsed query array will have: ('service_key'='AKEY', 'controller'=>'hi', 'version'='v1', 'action'=>'hello'). Obviously, the intention of inserting the service_key parameter in the route is to offer some kind of validation of the URLs to match a certain key. Of course, it is the developer's job to implement such a validation logic probably by implementing a wlRestTrigger trigger (see Creating triggers ). The commercial package do offer this functionality as a trigger implemented by wlRestTriggerServiceKey class.

4. Define custom output handlers (optional)

class csvSampleOutputHandler extends wlRestOutputHandler {

For more information, please refer to Creating output handlers .

5. Define custom triggers (optional)

class ipRejectSampleTrigger extends wlRestTrigger {

For more information, please refer to Creating triggers .

6. The endpoint API PHP script (actual service)

The endpoint should be a single PHP file (ex. index.php or endpoint.php) and should not render any output.

//create a service object
$service = new wlRestService();

//load a settings file (new settings will be merged with any previous loaded settings)
$service->loadSettings(__DIR__ . '/custom-rest-settings.ini');

//register the service controllers
$service->registerController(new sampleHelloRestControllerV1());
$service->registerController(new sampleHelloRestControllerV2());

//register custom output handlers with the extension (here is .csv)
$service->registerOutputHandler(new csvSampleOutputHandler('csv'));

//register global events
$service->registerTrigger(new ipRejectSampleTrigger(), wlRestTrigger::ON_REQUEST_EVENT);
$service->registerTrigger(new wlRestTriggerError(), wlRestTrigger::ON_ERROR_EVENT);

//setup authentication if needed
$authHandler = new myAuthHandler('localhost', 'my_database', 'root', 'secret', 'users', 'username', 'password', true);
$service->registerController(new wlRestControllerAuth($authHandler));
$service->registerTrigger(new wlRestTriggerAuth($authHandler), wlRestTrigger::ON_REQUEST_EVENT);

//setup optional caching for GET request types (here is set to 10 seconds)
$service->setCacheDir(__DIR__ . '/cache', 10);

//setup optional logging

//run the service

Caching and logging are optional.
By default any REST service registers the following output handlers: wlRestOutputHandlerJson, wlRestOutputHandlerXml, wlRestOutputHandlerPhp, wlRestOutputHandlerArray and wlRestOutputHandlerPlain.
To make URLs accessing the service more friendly, the developer can use .httaccess to setup some rewrite rules if Apache mod_rewrite is active. The commercial package includes a fully functional demo that shows a .httaccess configuration also.

Regular License $10.00
Use by you or one client, in a single end product which end users are not charged for.

Extended License $50.00
Use by you or one client, in a single end product which end users can be charged for.

Short Information

Building a RESTful web service for your PHP web application has never been so easy!
WiseLoop PHP REST Services is a powerful API framework that allows easy development of any kind of API through RESTful web services.

Buyer rating:
331 Sales