Untitled
unknown
plain_text
3 years ago
8.3 kB
22
Indexable
<?php
/**
* Tells the browser to allow code from any origin to access
*/
header("Access-Control-Allow-Origin: *");
/**
* Tells browsers whether to expose the response to the frontend JavaScript code
* when the request's credentials mode (Request.credentials) is include
*/
header("Access-Control-Allow-Credentials: true");
/**
* Specifies one or more methods allowed when accessing a resource in response to a preflight request
*/
header("Access-Control-Allow-Methods: POST, GET, PUT, DELETE");
/**
* Used in response to a preflight request which includes the Access-Control-Request-Headers to
* indicate which HTTP headers can be used during the actual request
*/
header("Access-Control-Allow-Headers: Content-Type");
require_once 'vendor/thingengineer/mysqli-database-class/MysqliDB.php';
class API {
public $db;
public function __construct()
{
$this->db = new MysqliDB('localhost', 'root', '', 'employee');
}
/**
* HTTP GET Request
*
* @param $payload
*/
public function httpGet($payload = array()){
// execute query
$query = $this->db->get('information');
$exist = $this->db->where('id', 31);
if($exist){
//check if query is success or fail
if ($query) {
echo json_encode(array(
'method' => 'GET',
'status' => 'success',
'data' => $query,
));
} else {
echo json_encode(array(
'method' => 'GET',
'status' => 'fail',
'data' => [],
'message' => 'Failed to Fetch'
));
}
}
}
/**
* HTTP POST Request
*
* @param $payload
*/
public function httpPost($payload)
{
// Check if payload contains null values
foreach ($payload as $value) {
if ($value === null) {
return json_encode(array(
'method' => 'POST',
'status' => 'fail',
'data' => [],
'message' => 'Payload contains null values'
));
}
}
if (empty($payload)) {
echo json_encode(['error' => 'Payload contains null values']);
}
//Execute Query
$query = $this->db->insert('information', $payload);
//check if query is success or fail
if ($query) {
echo json_encode(array(
'method' => 'POST',
'status' => 'success',
'data' => $payload,
));
}
else {
echo json_encode(array(
'method' => 'POST',
'status' => 'fail',
'data' => [],
'message' => 'Failed to Insert'
));
}
}
/**
* HTTP PUT Request
*
* @param $id
* @param $payload
*/
public function httpPut($id, $payload){
// Check if the ID in the payload matches the ID in the URL
if ($id != $payload['id']) {
echo json_encode(array(
'method' => 'PUT',
'status' => 'fail',
'data' => [],
'message' => 'Id doesnt match',
));
}
// Check if any values are present in the payload other than the 'id' field
unset($payload['id']);
if (empty($payload)) {
echo json_encode(array(
'method' => 'PUT',
'status' => 'fail',
'data' => [],
'message' => 'No fields to update',
));
}
// select ID
$this->db->where('id', $payload, 'IN')->update('information', $payload);
if ($this->db->getLastErrno() === 0)
echo 'Update succesfull';
else
echo 'Update failed. Error: '. $this->db->getLastError();
//execute query
//$query = $this->db->update('information', $payload);
//check if query is success or fail
// if ($query) {
// return json_encode(array(
// 'method' => 'PUT',
// 'status' => 'success',
// 'data' => $payload,
// ));
// }
// else {
// return json_encode(array(
// 'method' => 'PUT',
// 'status' => 'fail',
// 'data' => [],
// 'message' => 'Failed to Update'
// ));
// }
}
/**
* HTTP DELETE Request
*
* @param $id
* @param $payload
*/
public function httpDelete($id, $payload)
{
if (empty($id) || !is_numeric($id)) {
return json_encode(array(
'method' => 'DELETE',
'status' => 'fail',
'data' => [],
'message' => 'Invalid ID provided'
));
}
// Explode the ids
$payload = ['id' => is_string($id) ? explode(",", $id) : null];
// Check if there are any selected ids in the $payload array
if (count($payload['id'])) {
// If there are, use the IN operator to search for those specific ids in the 'id' column
$this->db->where('id', $payload['id'], 'IN');
} else {
// If there are no selected ids, use the normal operator to search for the single id in the 'id' column
$this->db->where('id', $id);
}
// Check if there are any selected ids in the $payload array
if (isset($payload['id']) && count($payload['id'])) {
// If there are, use the IN operator to search for those specific ids in the 'id' column
$this->db->where('id', $payload['id'], 'IN');
} else {
// If there are no selected ids, return an error message
return json_encode(array(
'method' => 'DELETE',
'status' => 'fail',
'data' => [],
'message' => 'No ID provided'
));
}
try{
// Execute query
$query = $this->db->delete('information', $id);
// check if success or fail
if ($query) {
echo json_encode(array(
'method' => 'DELETE',
'status' => 'success',
'data' => $payload,
));
return;
} else {
return json_encode(array(
'method' => 'DELETE',
'status' => 'fail',
'data' => [],
'message' => 'Failed to Delete'
));
}
}
catch(Exception $e)
{
return 'Message: ' .$e->getMessage();
}
}
//end
}
$request_method = $_SERVER['REQUEST_METHOD'];
// For GET,POST,PUT & DELETE Request
if ($request_method === 'GET') {
$received_data = $_GET;
} else {
//check if method is PUT or DELETE, and get the ids on URL
if ($request_method === 'PUT' || $request_method === 'DELETE') {
$request_uri = $_SERVER['REQUEST_URI'];
$ids = null;
$exploded_request_uri = array_values(explode("/", $request_uri));
$last_index = count($exploded_request_uri) - 1;
$ids = $exploded_request_uri[$last_index];
}
//payload data
$received_data = json_decode(file_get_contents('php://input'), true);
}
$api = new API;
//Checking if what type of request and designating to specific functions
switch ($request_method) {
case 'GET':
$api->httpGet($received_data);
break;
case 'POST':
$api->httpPost($received_data);
break;
case 'PUT':
$api->httpPut($ids, $received_data);
break;
case 'DELETE':
$api->httpDelete($ids, $received_data);
break;
}
?>Editor is loading...