Problem z REST API

0

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"]);
}


?>
1

A sprawdzałes chociaż co tkwi w zmiennej $_SERVER['REQUEST_METHOD']?

Poza tym użyj switcha zamiast tych if'ow, będzie ładniej wyglądało.
Nie zauważyłeś też, że w każdym ifie dajesz ten sam kod?
Wszędzie dajesz ten sam header i inny kod, przecież to aż się prosi by przygotować metode/funkcje która będzie generowala response. A jak masz ten fragment if (!$access) {... to tam już nie musisz nagłówka z json'em wysyłać? :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1