How to Create a Restful API in PHP Codeigniter 3.
in PHP
Hello everyone.
In this discussion, we will look at how to create a RESTFUL API with PHP Codeigniter 3.
Please download the above zip file and place or copy the files in the locations specified below.
- page rest.php in the application/config/ folder,
- In the application/libraries/ Folder, place REST_Controller.php and Format.php.
After placing the files in their proper locations, we simply need to import the file into the controller, as shown below.
require APPPATH . 'libraries/REST_Controller.php'; class Item extends REST_Controller { public function __construct() { parent::__construct(); $this->load->database(); } }
Let's look at an example Rest API function below, such as,
require APPPATH . 'libraries/REST_Controller.php'; class Item extends REST_Controller { public function __construct() { parent::__construct(); $this->load->database(); } public function index_put($id) { $UPDATE_DATA["name] = "Change Name; $UPDATE_WHERE_DATA["ID"] = "20"; $this->db->update('items', $UPDATE_DATA, $UPDATE_WHERE_DATA); $this->response(['Item updated successfully.'], REST_Controller::HTTP_OK); } }
A web API allows you to interact with a web server via HTTP requests, whereas a REST API allows you to interact with any type of server via HTTP. REST APIs are HTTP-based web services that provide an interface for clients to interact with the service.
Thank you so much.
Tagged: