nitiphone2021 Posted March 26, 2021 Share Posted March 26, 2021 Dear Friends. Just now I tried to create PHP to make Rest API for my mobile application and I would like to know that it's good coding and need any improve? it is my first time for Rest API. normally I just connect to PHP file directly on .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9A-Za-z_]+)$ function.php?func=$1 [L,QSA] RewriteRule ^([0-9A-Za-z_]+)/$ function.php?func=$1 [L,QSA] on function.php $func = $_GET['func']; switch( $func) { case 'lab_info': //ໜ້າຫຼັກ define( '_FUNC', 'lab_info.php'); break; case 'customer_info': //ຂໍ້ມູນຜູ້ໃຊ້ define( '_FUNC', 'customer_info.php'); break; default: // No function define( '_FUNC', 'functionnotfound.php'); break; } if( defined( '_FUNC') && constant( '_FUNC') !='') require( "func/" . _FUNC); on customer_info.php $error = array(); if(isset($_POST['user']) != 'lung'){ $error['status'] = "422"; $error['title'] = "Authentication Fail"; $error['detail'] = "Invalid user authentication"; echo json_encode($error);die(); } if(!isset($_POST['user'])){ $error['status'] = "401"; $error['title'] = "Invalid Attribute"; $error['detail'] = "Invalid Attribute For Information"; echo json_encode($error);die(); } $result = $conn->query("select Lab_Name,Lab_Username,Lab_Password from tb_labmanagers"); $customer = array(); while($row =mysqli_fetch_assoc($result)) { $customer[] = $row; } echo json_encode($customer); this image attached file is result from postman, client need to send user = 'lung' to get information Quote Link to comment https://forums.phpfreaks.com/topic/312383-create-php-for-rest-api-concept-need-someone-review-my-coding/ Share on other sites More sharing options...
requinix Posted March 26, 2021 Share Posted March 26, 2021 39 minutes ago, nitiphone2021 said: on .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9A-Za-z_]+)$ function.php?func=$1 [L,QSA] RewriteRule ^([0-9A-Za-z_]+)/$ function.php?func=$1 [L,QSA] RewriteConds only apply to the single next RewriteRule. They are not being applied to the second one. You can combine both RewriteRules by making the trailing slash optional, as in /? 42 minutes ago, nitiphone2021 said: on function.php Why use a _FUNC constant? Just require each file inside the switch and be done with it. 42 minutes ago, nitiphone2021 said: on customer_info.php if(isset($_POST['user']) != 'lung'){ Copy/paste fail? isset() returns true or false. It does not return the value so comparing it to 'lung' does not work. Make the !isset check be first, then change this check to use a regular comparison. echo json_encode($error);die(); If you are sending JSON then you need to include a Content-Type header with the value of "application/json". Additionally, if you want to include a HTTP status code like 422 or 401 then you should actually send a HTTP response code. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312383-create-php-for-rest-api-concept-need-someone-review-my-coding/#findComment-1585408 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.