Loading Depth Study
Preparing your workspace
PHP can act as an API backend by processing HTTP requests and returning structured JSON responses with appropriate status codes.
A PHP API commonly receives HTTP requests, validates input, performs business logic, accesses a database, and returns JSON. The response should include a meaningful HTTP status code and a predictable JSON structure. REST-style APIs typically use resource-oriented URLs and HTTP methods such as GET, POST, PATCH, and DELETE. Error responses should be consistent and should not expose internal implementation details.
A React frontend can call a PHP endpoint to retrieve courses and receive a JSON array instead of an HTML page.
<?php
header('Content-Type: application/json');
$response = [
'success' => true,
'data' => [
'id' => 1,
'title' => 'PHP Basics'
]
];
echo json_encode($response);The server returns a JSON response containing a course object.