PHP REST Services

PHP REST Services

RESTful web service API framework for PHP

Creating triggers

Triggers provides means to execute user actions when specific events happens. To build a trigger the developer must extend wlRestTrigger class and overwrite the wlRestTrigger::run method.
After writing the trigger, in order to make it functional, it must be registered in within a service using wlRestService::registerTrigger method.

1. How to use REST triggers to reject requests coming from a fixed IP address

class ipRejectSampleTrigger extends wlRestTrigger {
    public function run() {
        $ip = $this->getService()->getRequest()->getRemoteAddress();
        if($ip == '133.133.133.133') {
            throw new wlRestException("Requests from $ip are not allowed", 406);
        }
    }
}

This trigger should be called at request time so we will register it with wlRestTrigger::ON_REQUEST_EVENT event:

 $service = new wlRestService();                                                            //create a service
 $service->registerTrigger(new ipRejectSampleTrigger(), wlRestTrigger::ON_REQUEST_EVENT);   //register the trigger
 $service->run();                                                                           //run the service

2. How to use REST triggers to reject requests coming from IP addresses specified in settings

In this sample, the list of banned IPs is taken from settings under the banned_ips key.

class ipRejectSampleTrigger extends wlRestTrigger {
    public function run() {
        $banned = $this->getService()->getSettings()->getValue('banned_ips');
        if($banned) {
            $banned = ',' . str_replace(array(';', '|', ' '), ',', $banned) . ',';
        }
        $ip = $this->getService()->getRequest()->getRemoteAddress();
        if(strpos($banned, ",$ip,") !== false) {
            throw new wlRestException("Requests from $ip are not allowed", 406);
        }
    }
}

Loading the custom settings containing also the banned IP list is done by calling wlRestService::loadSettings:

 $service = new wlRestService();                                                            //create a service
 $service->loadSettings('custom-rest-settings.ini');                                        //load custom settings
 $service->registerTrigger(new ipRejectSampleTrigger(), wlRestTrigger::ON_REQUEST_EVENT);   //register the trigger
 $service->run();                                                                           //run the service

The 'custom-rest-settings.ini' can look like:

banned_ips=133.133.133.133,144.144.144.144
some_value=abc
some_other_value=133

3. How to use REST triggers to alter returned data

In this sample, we will add a timestamp column to the returned data.

class addTimestampSampleTrigger extends wlRestTrigger {
    public function run() {
        $data = $this->getService()->getData();     //get the original data
        $data = wlRestUtils::encodeToArray($data);  //make sure the data is an array
        $data['_timestamp'] = date('Y m d H:i:s');  //add the timestamp column
        $this->getService()->setData($data);        //inject the data back to the service in order to be served
    }
}

This trigger does data processing so it should be registered with the wlRestTrigger::ON_DATA_EVENT event:

 $service = new wlRestService();                                                            //create a service
 $service->registerTrigger(new addTimestampSampleTrigger(), wlRestTrigger::ON_DATA_EVENT);  //register the trigger
 $service->run();                                                                           //run the service

4. How to use REST triggers to catch and format exceptions

If something wrong happens during a REST call, the exception is catched and a standard message is delivered to the requester. It would be helpful if more detailed information is delivered so the service consumer can take proper actions.
This sample will show how to return a nicely formatted error messages containing also the call stack.

class wlRestTriggerFormatError extends wlRestTrigger {
    public function run() {
        $ex = $this->getService()->getData();                   //the service data is in fact the exception
        $code = $ex->getCode();                                 //get the exception code
        $message = wlRestUtils::getStatusCodeMessage($code);    //get the exception message (by its code)
        return '[' . $code . '] ' . $message . ' in: ' . $ex->getFile() . ' at line ' . $ex->getLine() . ', stack:' . $ex->getTraceAsString();
    }
}

This trigger does error processing so it should be registered with the wlRestTrigger::ON_ERROR_EVENT event:

 $service = new wlRestService();                                                            //create a service
 $service->registerTrigger(new wlRestTriggerFormatError(), wlRestTrigger::ON_ERROR_EVENT);  //register the trigger
 $service->run();                                                                           //run the service

The commercial package do offer a nice implementation of an error formatting trigger: wlRestTriggerError.

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