There are two steps in the API:
1.) Request with a username and password is sent like this:
curl -X 'POST' \
'https://api.webpage.com' \
-H 'accept: text/plain' \
-H 'Content-Type: application/json' \
-d '{
"username": "string",
"password": "string"
}'
1.1) Server response should be:
response body
{
"token": "33670dc8-1f24f-4482-2402f-d126t46tbdd7"
}
response headers
content-type: application/json; charset=utf-8
date: Fri,14 Jul 2023 12:53:57 GMT
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
2.) As we get the token, the second request is as follows:
curl -X 'POST' \
'https://api.webpage.com' \
-H 'accept: text/plain' \
-H 'Content-Type: application/json' \
-d '{
"token": "string",
"name": string,
"surname": "string"
}'
2.1) And the final response should be:
response body
{
"age": 46
}
response headers
content-type: application/json; charset=utf-8
date: Fri,14 Jul 2023 12:53:57 GMT
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
Question/Problem Not sure how to create this database and PHP scripts to correctly respond to the above scenario.
Until now I've created a MySQL database with the data, and PHP script which connects to the database and displays the whole dataset. But how do I incorporate authentication step, and display only the needed data ("age") if other two data ("name", "surrname") are sent to the API?
My SQL database schema:
table 1
"id", "name", "surname", "age"
"0", "Adam", "Smith", "46"
...
table 2
"id", "username", "password", "token"
"0", "user", "pass", "33670dc8-1f24f-4482-2402f-d126t46tbdd7"
My php script:
<?php
class API {
function Select(){
$conn = mysqli_connect("mysqlserver.com", "root", "", "api_database");
if($conn == false){
// reconnect
}
else{
echo "connected.. \n";
}
$sqlq = "SELECT * FROM table 1 ORDER BY id";
$results = $conn-> query($sqlq);
$data = array();
while($OutputData = $results->fetch_assoc()){
$data[$OutputData['id']] = array(
'id' => $OutputData['id'],
'name' => $OutputData['name'],
'surname' => $OutputData['surname'],
'age' => $OutputData['age']
);
}
return json_encode($data);
}
}
$API = new API;
header('Content-Type: application/json');
echo $API->Select();
?>
My current curl response (with still no authentication step incorporated):
connected.. [{"id":"0","name":"Adam","surname":"Smith","age":"46"},{"id":"1","name":"Jessica","surname":"Smith","age":"44"}, ...]
Am I even going in the right direction? Stuck here after my basic PHP knowledge ran out.