voici un exemple de simple code
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
handleGetRequest();
break;
case 'POST':
handlePostRequest();
break;
default:
handleInvalidRequest();
break;
}
function handleGetRequest() {
echo json_encode(array("message" => "Handling GET request."));
}
function handlePostRequest() {
$data = json_decode(file_get_contents("php://input"));
echo json_encode(array("message" => "Handling POST request.", "data" => $data));
}
function handleInvalidRequest() {
http_response_code(405);
echo json_encode(array("message" => "Invalid request method."));
}