php simple rest api – create php simple rest api example and demo
In this post we will show you php simple rest api – create php simple rest api example and demo, hear for php simple rest api – create php simple rest api example and demo we will give you demo and example for implement.
we are the author of php-crud-api and we want to share the core of the application with you. It includes routing a JSON REST request, converting it into SQL, executing it and giving a meaningful response.
Code for create php simple rest api example and demo
<?php // hear we get the HTTP method, path and body of request $request_method = $_SERVER['REQUEST_METHOD']; $request_data = explode('/', trim($_SERVER['PATH_INFO'],'/')); $get_input = json_decode(file_get_contents('php://input'),true); // hear mysql database connect $connect_link = mysqli_connect('localhost', 'root-user', 'password', 'onlinecode-db'); mysqli_set_charset($connect_link,'utf8'); // hear retrieve the table and key from the path $table_name = preg_replace('/[^a-z0-9_]+/i','',array_shift($request_data)); $set_key = array_shift($request_data)+0; // hear escape the columns and values from the input object $columns_val = preg_replace('/[^a-z0-9_]+/i','',array_keys($get_input)); $input_values = array_map(function ($get_value) use ($connect_link) { if ($get_value===null) return null; return mysqli_real_escape_string($connect_link,(string)$get_value); },array_values($get_input)); // hear build the SET part of the SQL command $set = ''; for ($i=0;$i<count($columns_val);$i++) { $set.=($i>0?',':'').'`'.$columns_val[$i].'`='; $set.=($input_values[$i]===null?'NULL':'"'.$input_values[$i].'"'); } // hear create SQL based on HTTP request_method switch ($request_method) { case 'GET': $sql_query = "select * from `$table_name`".($set_key?" WHERE id=$set_key":''); break; case 'PUT': $sql_query = "update `$table_name` set $set where id=$set_key"; break; case 'POST': $sql_query = "insert into `$table_name` set $set"; break; case 'DELETE': $sql_query = "delete `$table_name` where id=$set_key"; break; } // hear excecute SQL statement $get_result = mysqli_query($connect_link,$sql_query); // hear die if SQL statement failed if (!$get_result) { http_response_code(404); die(mysqli_error()); } // hear print results, insert id or affected row count if ($request_method == 'GET') { if (!$set_key) echo '['; for ($i=0;$i<mysqli_num_rows($get_result);$i++) { echo ($i>0?',':'').json_encode(mysqli_fetch_object($get_result)); } if (!$set_key) echo ']'; } elseif ($request_method == 'POST') { echo mysqli_insert_id($connect_link); } else { echo mysqli_affected_rows($connect_link); } // hear connection close for mysql mysqli_close($connect_link);
how to Run this php smiple rest api example and demo
Save this file as “api.php” in your (Apache) document root and decision it using:
[/php]
Or you will use the PHP intrinsic webserver from the statement using: $ php -S localhost:8888 api.php
The URL once ran in from the statement is:
http://onlinecode:8888/api.php/{$table}/{$id}
NB: Don’t forget to regulate the ‘mysqli_connect’ parameters within the on top of script!
REST API during a single PHP file
Although the on top of code isn’t good it really will do three vital things:
- Support HTTP verbs GET, POST, UPDATE and DELETE
- Escape all knowledge properly to avoid SQL injection
- Handle null values correctly
One might so say that the remainder API is absolutely useful. you will run into missing options of the code, such as:
- No permission system
- No column choice supported
- No connected knowledge (automatic joins) supported
- No page number or sorting supported
- No condensed JSON output supported
- No search/filter support
- No support for PostgreSQL or SQL Server
- No base64 binary column support
- No POST parameter support
- No JSONP/CORS cross domain support
Hope this code and post will helped you for implement php simple rest api – create php simple rest api example and demo. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org