Jump to content

Recommended Posts

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

 

API.png

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.