Please I am new to web service and needed to make one now for my firm. The service is to enable client to send HTTP request by passing these parameters (e.g. api.example.com/get.php?para1=1234¶2=keyword) to retrieve specific record and to alter user's status through another call (e.g. api.example.com/alter.php?para1=1234¶2=keyword). I came across a simple JSon + php script which i followed to make mine and it work fine. But now i'm instructed to hash the Request / Response parameters with SHA512 and if possible provide an authentication token. below is my code:
CODE TO RETRIEVE RECORD (get.php)
// CODE TO RETRIEVE RECORD (get.php)
// Include db_connection.php
include_once('db_conn.php');
$para1= isset($_REQUEST['para1']) ? mysqli_real_escape_string($conn,$_REQUEST['para1']) : "";
$para2= isset($_REQUEST['para2']) ? mysqli_real_escape_string($conn,$_REQUEST['para2']) : "";
if(!empty($para1) && !empty($para2)){
$q = mysqli_query($conn,"select column1, column2 from `table_name` WHERE para1='$para1' AND para2e= '$para2'");
$result =array();
while($r = mysqli_fetch_array($q)){
extract($r);
$result = array("Name" => $column1, 'Other Detail' => $column2);
}
//$json = array("Detail" => $result);
$json = $result ;
}else{
$json = array("status" => 0, "msg" => "Parameter not define");
}
@mysqli_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json, JSON_PRETTY_PRINT);
CODE TO ALTER STATUS FROM PASSIVE TO ACTIVE (alter.php)
//CODE TO ALTER STATUS FROM PASSIVE TO ACTIVE (alter.php)
// Include confi.php
include_once('db_conn.php');
if($SERVER['REQUEST_METHOD'] == "GET"){
$para1= isset($REQUEST['para1']) ? mysqli_real_escape_string($conn,$REQUEST['para1']) : "";
$para2= isset($REQUEST['para2']) ? mysqli_real_escape_string($conn,$_REQUEST['para2']) : "";
// Add your validations
if(!empty($para1) && !empty($para2)){
$active = 1 ;
$q = mysqli_query($conn,"UPDATE table_name SET Status = '$active' WHERE para1='$para1' AND para2= '$para2'"); if($q)
{
$json = array("MSG" => "Active Altered!!.");
}
else{
$json = array("Status" => 0, "MSG" => "Error processing");
}
}
else{
$json = array("status" => 0, "MSG" => "Parameter not define");
}
}
else{
$json = array("status" => 0, "MSG" => "Parameter not define");
}
@mysqli_close($conn);
}}
/ Output header /
header('Content-type: application/json');
echo json_encode($json,JSON_PRETTY_PRINT);