An API entry point is the PHP file through which a client starts communicating with the REST API. It receives the HTTP request, processes the required information, and sends a response back to the client.
An API entry point is a URL or PHP endpoint where an API request begins.
For example:
http://localhost/student_api/api/students.php
The client sends an HTTP request to this endpoint.
The client needs a specific endpoint to communicate with the backend.
React Native
↓
API Entry Point
↓
PHP
↓
MySQL
The entry point receives the request and starts the API processing.
Suppose our student endpoint is:
api/students.php
The complete local URL may be:
http://localhost/student_api/api/students.php
This file can process student-related API requests.
Create a file named students.php inside the API folder.
student_api/
│
└── api/
└── students.php
This file will become our main student API endpoint.
A very simple PHP API entry point can start like this:
<?php
header("Content-Type: application/json");
echo json_encode([
"success" => true,
"message" => "Student API is working"
]);
The endpoint sends a JSON response to the client.
REST APIs commonly return JSON data. PHP can tell the client that the response is JSON using the Content-Type header.
header("Content-Type: application/json");
This should be sent before outputting the JSON response.
PHP can convert an array into JSON using json_encode().
$response = [
"success" => true,
"message" => "API is working"
];
echo json_encode($response);
For a simple GET endpoint, you can open the API URL in a browser.
http://localhost/student_api/api/students.php
If the PHP file is working, it should display a JSON response.
Postman is a better tool for testing different HTTP methods and request bodies.
GET
http://localhost/student_api/api/students.php
Click Send and check the response.
PHP provides the $_SERVER['REQUEST_METHOD'] value to identify the HTTP method used by the request.
$method = $_SERVER['REQUEST_METHOD'];
echo $method;
For a GET request, the value will be GET.
We can check whether the client sent a GET request.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo json_encode([
"message" => "GET request received"
]);
}
The same entry point can also check for POST requests.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo json_encode([
"message" => "POST request received"
]);
}
This allows one endpoint to handle different operations.
An endpoint can use conditional statements to handle multiple HTTP methods.
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
// Get students
} elseif ($method === 'POST') {
// Add student
}
The same approach can be used for PUT, PATCH, and DELETE requests.
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
// Read
break;
case 'POST':
// Create
break;
case 'PUT':
// Update
break;
case 'DELETE':
// Delete
break;
}
A switch statement can make the API entry point easier to organize when multiple HTTP methods are supported.
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
break;
case 'POST':
break;
case 'PUT':
break;
case 'DELETE':
break;
default:
break;
}
PHP can set an HTTP response status code using http_response_code().
http_response_code(200);
For example, 200 can indicate that a request was successfully processed.
http_response_code(200);
echo json_encode([
"success" => true,
"message" => "API is working"
]);
The client receives both the HTTP status and the JSON response body.
If the API does not support the requested HTTP method, it can return 405 Method Not Allowed.
http_response_code(405);
echo json_encode([
"success" => false,
"message" => "Method not allowed"
]);
<?php
header("Content-Type: application/json");
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
echo json_encode([
"success" => true,
"message" => "GET request"
]);
break;
case 'POST':
echo json_encode([
"success" => true,
"message" => "POST request"
]);
break;
default:
http_response_code(405);
echo json_encode([
"success" => false,
"message" => "Method not allowed"
]);
}
Once the entry point is working, we can include our database connection.
require_once '../config/database.php';
The API can then use the PDO connection to communicate with MySQL.
A basic API endpoint can load the database connection and then process the request.
<?php
header("Content-Type: application/json");
require_once '../config/database.php';
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
// Database operation
}
GET Request
↓
students.php
↓
Check HTTP Method
↓
Connect to MySQL
↓
Execute SELECT Query
↓
Create JSON
↓
Return Response
POST Request
↓
students.php
↓
Read JSON Body
↓
Validate Data
↓
Insert into MySQL
↓
Create JSON Response
↓
Return Response
React Native will send HTTP requests to the PHP entry point.
React Native
↓
fetch() / Axios
↓
students.php
↓
PHP
↓
MySQL
↓
JSON
↓
React Native
The mobile application communicates with the API rather than directly with the database.
A REST API endpoint should return API data in a consistent format.
Example:
{
"success": true,
"message": "Students retrieved successfully",
"data": []
}
Avoid printing unrelated HTML, warnings, or debugging text in the API response.
Use Postman to test the endpoint.
GET
http://localhost/student_api/api/students.php
Then check:
Receive Request
↓
Set JSON Header
↓
Load Configuration
↓
Connect Database
↓
Identify HTTP Method
↓
Validate Request
↓
Perform Operation
↓
Set Status Code
↓
Return JSON
student_api/
│
├── config/
│ └── database.php
│
├── api/
│ └── students.php
│
└── index.php
↓
React Native
↓
students.php
↓
database.php
↓
MySQL
This is the basic connection between the mobile application, API entry point, database configuration, and MySQL database.
The API entry point is an important part of a REST API. It receives the client request, identifies the HTTP method, communicates with the database when required, and returns a JSON response. Our students.php endpoint will become the main entry point for student-related API operations.
Client
↓
students.php
↓
HTTP Method
↓
PHP Logic
↓
MySQL
↓
JSON Response
Question: Which PHP variable can be used to identify the HTTP method of an API request?