One of the most popular types of APIs, REST or RESTful APIs are designed to take advantage of existing protocols. The app comes with a ready-to-use RestFul Service. You can write code in the REST section and integrate with other applications.
The app comes with pre-installed REST drivers. You can use the application according to your requirements.
Example GET Function is like this;
public function clients_get()
{
$id = $this->get('id');
if ($id === NULL) {
$list = $this->restservice->customers();
if ($list) {
// Set the response and exit
$this->response($list, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
} else {
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
How to Enable REST Service
To enable the REST service, you need to create an API key in Settings > REST API.
Answer
The default data response will be in JSON format.
Methods
Some basic functions are added by default. You can edit the `application/controller/Rest.php` file to add more methods.
Example POST method;
public function clients_post()
{
$id = $this->post('id');
if ($id === NULL) {
$list = $this->restservice->customers();
// Check if the users data store contains users (in case the database result returns NULL)
if ($list) {
// Set the response and exit
$this->response($list, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
} else {
// Set the response and exit
$this->response([
'status' => FALSE,
'message' => 'No users were found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
// Find and return a single record for a particular user.
$id = (int)$id;
// Validate the id.
if ($id <= 0) {
// Invalid id, set the response and exit.
$this->response(NULL, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
}
// Get the user from the array, using the id as key for retrieval.
// Usually a model is to be used for this.
$list = $this->restservice->customers($id);
if (!empty($list)) {
$this->set_response($list[0], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
} else {
$this->set_response([
'status' => FALSE,
'message' => 'User could not be found'
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
}
}
<!-- end row -->