RESTp

RESTful web service in PHP for performing the CRUD


Thank you for purchasing my plugin. If you have any questions that are beyond the scope of this help file, please feel free to email. Thanks so much!

RESTp is an advanced RESTFul Web service developed using PHP that helps you to perform crud operation. It is build on top of the popular database abstraction script "PDOModel". It is developed using PHP and can support many different database (Mysql, sqlite, pgsql and MSSQL).

Most of the Mobile and Web Apps requires communication with server using some API to send and received data. REST is by far the most commonly-used style for designing APIs, especially in the mobile world. These Mobile and web apps often requires to perform crud operation i.e Insert data, read data, update data and delete data using API. To write code for the crud operations for each individual mobile app and web app is quite cumbersome task. That’s where RESTp comes into action. RESTp provides a single application based on REST architecture that helps you to perform CRUD operation on any kind of mobile and web apps. All you need to do is to connect it to your database and it automatically generates all kind of resources to perform CRUD operation.
## Requirements
  1. - PHP 5.3 or higher
  2. PDO extension enabled
  3. CURL Enabled if you want to connect using CURL PHP
  1. Download the folder and extract it.
  2. Main files that you are going to use in your application, are in the api folder.
  3. Demo and documentation folder can be used for reference purpose. They are not required for your application.
  1. Copy api folder in your application.
  2. Open api/config/config.php file.
  3. Enter database settings in config.php (hostname, dbname, username, pwd, purchase code etc)
  4.                             //Set the host name to connect for database
                                $config["hostname"] =  "localhost";
                                //Set the database name
                                $config["database"] = "pdocrud";
                                //Set the username for database access
                                $config["username"] = "root";
                                //Set the pwd for the database user
                                $config["password"] = "";
                                //Set the database type to be used
                                $config["dbtype"] = "mysql";
                                //Please enter purchase code. Please check how to find purchase code details here https://help.market.envato.com/hc/en-us/articles/202822600-Where-Is-My-Purchase-Code-
                                $config["purchase_code"] = "";
                                //Set the character set to be used
                                $config["characterset"] = "utf8";
                            
  5. Now, you can call RESTp api using any standard codes to connect any rest api. You can use various demo code to test application.

** Please make sure to take complete database backup.

Please note that if you want to change the folder name of the application, make sure to change it in .htaccess file to allow the pretty urls access work.

RESTp allows to perform CRUD (Create, Read, Update, Delete) operation on all tables of database. For every tables in database, it provides endpoints to perform CRUD operation. An example of all RESTp api endpoints for a table are below


Example endpoint with table name "orders"

There are many more functionality you can perform, which we will explain later. Let's start looking all REST methods one by one.

  1. List all records of table
                            GET http://localhost/RestpAPI/api/orders/
    
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"39","order_no":"578","order_date":"2018-08-22","customer_name":"Xenos Clarke","order_amount":"630","order_status":"Completed"},
                            ...,}
                            
  2. Get record with primary key value 44 from database table name "orders".
                            GET http://localhost/RestpAPI/api/orders/44
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"44","order_no":"22241","order_date":"2018-08-22","customer_name":"Cecilia Carney","order_amount":"60","order_status":"Completed"}]}
                            
    Please note that table must have primary key else use the next example to specify the column to look for.
  3. Get record with column name "order_no" having value "50683" from database table name "orders".
                            GET http://localhost/RestpAPI/api/orders/order_no/50683
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"57","order_no":"50683","order_date":"2018-08-22","customer_name":"Hilary Conner","order_amount":"60","order_status":"Completed"}]}
                            
  4. Sort or order records by column name "order_date"
                            GET http://localhost/RestpAPI/api/orders/?orderby=order_date
                            GET http://localhost/RestpAPI/api/orders/?orderby[]=order_date&orderby[]=ID// as an array for multiple sorts columns
                            GET http://localhost/RestpAPI/api/orders/?orderby[]=order_date+desc // descending order
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"39","order_no":"578","order_date":"2018-08-22","customer_name":"Xenos Clarke","order_amount":"630","order_status":"Completed"},
                            ...,}
                            
  5. Search records - applying where condition You can pass 5 parameters in GET parameter of where where=param1,param2,param3,param4,param5 param1={col_name} param2={col_val} param3={operator} (eq, bt, in, nin etc) param4={and_or_condition} param5={(,)} (open or close bracket)
                            GET http://localhost/RestpAPI/api/orders/?where=order_no,2222 //default operator is =
                            GET http://localhost/RestpAPI/api/orders/?where=order_no,2222,eq //you can specify operator here
                            GET http://localhost/RestpAPI/api/orders/?where[]=order_no,2222:76778,bt //example of between
                            GET http://localhost/RestpAPI/api/orders/?where[]=order_no,2222,eq,and,(&where[]=order_no,76778,eq,or&where[]=order_no,2222,eq,,) //multiple where condition
                            //generated sql SELECT * FROM `orders` WHERE `order_no`= ? and ( `order_no`= ? or `order_no`= ? ) 
    
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"131","order_no":"2222","order_date":"2018-08-22","customer_name":"sdfsdfc","order_amount":"60","order_status":"Completed"}]}
                            
                            Available operators 
                            - lk: LIKE operator
                            - eq: = operator
                            - neq: != operator
                            - lt: < operator
                            - le: <= operator
                            - ge: >= operator
                            - gt: > operator
                            - bt: between operator
                            - in: in operator
                            - nin: Not in operator
                            
  6. Group by columns
                            GET http://localhost/RestpAPI/api/orders/?groupby=order_status
                            output:
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"39","order_no":"578","order_date":"2018-08-22","customer_name":"Xenos Clarke","order_amount":"630","order_status":"Completed"}..}]
                            
  7. Pagination - Apply Limit to no of records (offset, num of records)
                            GET http://localhost/RestpAPI/api/orders/?limit=0,10;
                            output:
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"39","order_no":"578","order_date":"2018-08-22","customer_name":"Xenos Clarke","order_amount":"630","order_status":"Completed"}..}]
                            
  8. Get selected columns of table.
                            GET http://localhost/RestpAPI/api/orders/?columns=ID,order_no;
                            output:
                            {"message":"Operation done successfully","error":null,"data":[{"ID":"39","order_no":"578"},..}                        
                            
  9. pass data array as GET parameter by encoding data as url format 4
                            $data = array("where" => array('order_no,2222,"eq"'),
                            "orderby" => array("order_amount asc", "order_status desc"),
                            "groupby" => array("order_status"));
                            $data = http_build_query($data);
    
                            GET http://localhost/RestpAPI/api/orders?".$data
                            Above example is in php language but can be achieved in other languages also.
                            {"message":"Operation done successfully","error":null,
                            "data":[{"ID":"131","order_no":"2222","order_date":"2018-08-22","customer_name":"sdfsdfc","order_amount":"60","order_status":"Completed"}]}
                            
  10. CREATE Record - POST Data Option 1 Send post data as x-www-form-urlencoded
                            POST http://localhost/RestpAPI/api/orders/
                            //data to be posted
                            "data[order_date]=2018-08-28&data[order_amount]=800&data[order_status]=Completed";
    
                            Output: It returns last insert id.
                            {"message":"Operation done successfully","error":null,"data":"281"}
                            
  11. CREATE Record - POST Data Option 2 Send post data as json
                            POST http://localhost/RestpAPI/api/orders/
                            //data to be posted
                            '{"data":{"order_date":"2018-08-28","order_amount":900,"order_status":"Completed"}}';
    
                            Output: It returns last insert id.
                            {"message":"Operation done successfully","error":null,"data":"281"}
                            
  12. Update Record - PUT Data Option 1 Send PUT data as x-www-form-urlencoded
                            POST http://localhost/RestpAPI/api/orders/
                            //data to be posted
                            "data[order_date]=2018-08-28&data[order_amount]=800&data[order_status]=Completed";
    
                            Output: It returns total number of rows updated.
                            {"message":"Operation done successfully","error":null,"data":1}
                            
  13. Update Record - PUT Data Option 2 Send PUT data as json
                            POST http://localhost/RestpAPI/api/orders/
                            //data to be posted
                            '{"data":{"order_date":"2018-08-28","order_amount":900,"order_status":"Completed"}}';
    
                            Output: It returns total number of rows updated.
                            {"message":"Operation done successfully","error":null,"data":1}
                            
  14. Delete Record - DELETE Option 1 Deletion of record is done with the DELETE method. It returns total number of records deleted. Using Primary key -If primary key value is passed in url then it will delete the matching primary key entry. Please make sure to provide where condition (either by ID in url or where condition) else it will delete all records of table.
                            DELETE http://localhost/RestpAPI/api/orders/57
    
                            //set CURLOPT_CUSTOMREQUEST =  DELETE to do a  HTTP DELETE
                            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                            
  15. Delete Record - DELETE Option 2 //Using where condition-If primary key value is passed in url then it will delete the matching primary key entry. Please make sure //to provide where condition (either by ID in url or where condition) else it will delete all records of table // Please note that where condition will replace the ID in the url..
                            //Dummy data to delete. Please note the format of data
                            DELETE http://localhost/RestpAPI/api/orders/57
                            {"where":["order_no,2222,"eq"]}
                            
RESTp supports various callback functions at various events that can be used to perform specific operations as required. Callback functions are very useful to perform special operation as per requirement. For example, if you want to change the insert data field e.g. password to md5 encrypted, you can use callback functions. You need to add call back function and needs to include function definition in the api/index.php file.
                    function beforeInsert($data, $obj) {  
                        return $data;
                    }
                    $restpAPI = new RESTpAPI();
                    $restpAPI->addCallback("before_insert", "beforeInsert");
                    $restpAPI->render();