Estructura básica API RESTful
<?php $Headers = getallheaders(); // Recibimos los HEADERS $Body = json_decode(file_get_contents('php://input')); // Recibimos el BODY // Seleccionamos el tipo de petición que nos llega switch($_SERVER['REQUEST_METHOD']){ case 'GET': // Acciones de ejemplo $Retorna = array( 'Peticion' => 'GET', 'Parametros' => $_GET, 'Headers' => $Headers, 'Body' => $Body ); header("HTTP/1.1 200 OK"); echo json_encode($Retorna); break; case 'POST': header("HTTP/1.1 200 OK"); break; case 'PUT': header("HTTP/1.1 200 OK"); break; case 'DELETE': header("HTTP/1.1 200 OK"); break; default: header("HTTP/1.1 400 Bad Request"); break; } /* Response Code Description 200 Success. 400 The requested entity does not exist, or the body of the POST request is not properly formatted. 401 Authentication failed, likely due to invalid credentials. 403 Access denied, likely due to insufficient permissions. 404 The API endpoint does not exist. 409 Account is undergoing daily maintenance (TestRail Cloud only). 429 API Rate limit reached. 500 Server error. */
Para usar URL “amigables” habría que agregar en el archivo .htaccess lo siguiente:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !\index.php$ RewriteRule ^([\w-]+)/?$ index.php?P1=$1 [QSA,L] RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?P1=$1&P2=$2 [QSA,L]