Napisałam następującą rest api, testowałam w postman i nie działa mi metoda get i put dla wskazanych rekordów oraz delete co robię nie tak?
<?php
try {
$db = new PDO('mysql:host=localhost;charset=utf8;dbname=results','root','');
}
catch(PDOException $e) {
echo $e->getMessage();
}
$access = false;
$token = json_decode(file_get_contents('php://input'), true);
if (isset($token['token'])) {
if ($token['token'] == "kdjjaskjdadjlkajdalksdjlasjclkxzmclm2l3k4j534lk643n6m345n34lk5joifvu90f8sd90fsudfjr4wrm345r9d9sf89ds0f8s09df8094w8r0r4mrnwemfzxxmvlzvhwdp") {
$access = true;
}
}
if (!$access) {
echo json_encode(['status' => "ACCESS DENIED"]);
die();
}
$method = $_SERVER['REQUEST_METHOD'];
if ($method === "GET") {
header('Content-Type: application/json');
$statement = $db->prepare("SELECT id, humidity, temperature FROM results");
if (isset($_GET["id"]) && !empty($_GET["id"])) {
$statement = $db->prepare("SELECT ID, humidity, temperature FROM results WHERE ID = :id");
$statement->bindValue(":id", $_GET["id"], PDO::PARAM_INT);
}
$statement->execute();
$tab = $statement->fetchAll(PDO::FETCH_ASSOC);
$output = json_encode($tab);
echo $output;
}
if ($method === "POST") {
header('Content-Type: application/json');
$json = json_decode(file_get_contents('php://input'), true);
$statement = $db->prepare("INSERT INTO results (humidity,temperature) VALUES (:humidity, :temperature)");
$statement->bindValue(":humidity", $json['humidity'], PDO::PARAM_STR);
$statement->bindValue(":temperature", $json['temperature'], PDO::PARAM_INT);
$statement->execute();
echo json_encode(['status' => "CREATED"]);
}
if ($method === "DELETE") {
header('Content-Type: application/json');
if (isset($_GET["id"])) {
$statement = $db->prepare("DELETE FROM results WHERE ID = :id");
$statement->bindValue(":id", $_GET["id"], PDO::PARAM_INT);
$statement->execute();
echo json_encode(['status' => "DELETED"]);
}
}
if ($method === "PUT") {
header('Content-Type: application/json');
if (isset($_GET["id"])) {
$json = json_decode(file_get_contents('php://input'), true);
$statement = $db->prepare("UPDATE results SET humidity=:humidity,temperature=:temperature WHERE ID = :id");
$statement->bindValue(":humidity", $json["humidity"], PDO::PARAM_STR);
$statement->bindValue(":temperature", $json["temperature"], PDO::PARAM_INT);
$statement->bindValue(":id", $_GET["id"], PDO::PARAM_INT);
$statement->execute();
}
echo json_encode(['status' => "UPDATED"]);
}
?>