Lesson 46 of 158 – PHP REST API Project
46%

PHP REST API Project

In this section, we will start building a practical REST API using PHP and MySQL. This API will later be connected with a React Native mobile application.

Note: In this project, PHP will work as the backend server, MySQL will store the data, and React Native will act as the mobile client.

1. What We Will Build

We will build a Student Management REST API.

The API will support:

  • Student registration
  • Get all students
  • Get a single student
  • Add student
  • Update student
  • Delete student
  • Search students

2. Technologies Used

This project will use:

  • PHP
  • MySQL
  • PDO
  • JSON
  • HTTP methods
  • Postman
  • React Native
  • TypeScript

3. Project Architecture

The basic architecture will be:

React Native Mobile App
          ↓
       REST API
          ↓
       PHP Backend
          ↓
        MySQL
       Database

The mobile application sends HTTP requests to the PHP API.

4. What is the Backend?

The backend is the server-side part of the application.

In this project, PHP will process requests from the mobile application and communicate with MySQL.

Mobile App
     ↓
PHP
     ↓
MySQL

5. What is the Database?

MySQL will store student information.

For example:

students

id
name
email
mobile
course

PHP will communicate with this database using PDO.

6. What is the API?

The API is the communication layer between the mobile application and the database.

React Native
      ↓
      API
      ↓
     PHP
      ↓
    MySQL

The mobile application does not directly connect to MySQL.

7. Why Use PHP for the API?

PHP is commonly used for server-side web development and can be used to create REST APIs.

PHP can receive HTTP requests, validate data, communicate with MySQL, and return JSON responses.

8. Why Use MySQL?

MySQL is a relational database management system that can store structured application data.

For our project, it will store student records.

Student
---------
ID
Name
Email
Mobile
Course

9. Why Use PDO?

PDO provides a consistent way for PHP applications to communicate with databases.

We will use PDO to connect PHP to MySQL and execute database queries.

PDO also supports prepared statements, which help protect database queries from SQL injection when used correctly.

10. Why Use JSON?

REST APIs commonly exchange structured data using JSON.

Example:

{
    "id": 1,
    "name": "Rahul Kumar",
    "course": "ADCA"
}

React Native can easily process JSON responses.

11. Development Environment

For local development, you can use a PHP development environment such as XAMPP.

The basic components are:

  • Apache
  • PHP
  • MySQL
  • phpMyAdmin

12. Project Folder

Create a project folder inside your local web server directory.

Example:

htdocs/
└── student_api/

The exact location depends on your PHP development environment.

13. Basic Project Structure

Our project can start with a simple structure.

student_api/
│
├── config/
├── api/
├── index.php
└── database.php

We will expand this structure as the project becomes larger.

14. Config Folder

The config folder can contain configuration-related files.

student_api/
└── config/
    └── database.php

The database connection can be kept separate from API endpoint logic.

15. API Folder

The API folder can contain endpoint files.

student_api/
└── api/
    ├── students.php
    ├── login.php
    └── register.php

Each endpoint can perform a specific API operation.

16. Database Connection

The API needs a database connection before it can read or write student records.

A PDO connection can be created using:

$pdo = new PDO(
    "mysql:host=localhost;dbname=student_api",
    "root",
    ""
);

In a real project, database credentials should be configured securely.

17. API Endpoint

An endpoint is a URL through which a client communicates with the API.

Example:

http://localhost/student_api/api/students.php

This endpoint can be used for student-related operations.

18. GET Endpoint

A GET request can be used to retrieve student records.

GET
http://localhost/student_api/api/students.php

The API can query MySQL and return the student records as JSON.

19. POST Endpoint

A POST request can be used to create a new student.

POST
http://localhost/student_api/api/students.php

The client can send student information as JSON.

20. PUT Endpoint

A PUT request can be used to update an existing student.

PUT
http://localhost/student_api/api/students.php?id=1

The API receives the updated information and modifies the database record.

21. DELETE Endpoint

A DELETE request can be used to remove a student.

DELETE
http://localhost/student_api/api/students.php?id=1

The API can delete the corresponding database record.

22. API Request Flow

React Native
      ↓
HTTP Request
      ↓
PHP API
      ↓
Validate Request
      ↓
MySQL
      ↓
JSON Response
      ↓
React Native

This is the basic flow of our application.

23. Testing with Postman

Before connecting the API to React Native, test the endpoints using Postman.

For example:

GET /students.php
POST /students.php
PUT /students.php?id=1
DELETE /students.php?id=1

Check the status code and JSON response for every request.

24. API Response

The PHP API should return structured JSON responses.

Example:

{
    "success": true,
    "message": "Student added successfully",
    "data": {
        "id": 1,
        "name": "Rahul Kumar"
    }
}

25. API Error Response

The API should also return useful error responses.

Example:

{
    "success": false,
    "message": "Student not found"
}

The HTTP status code should also represent the result appropriately.

26. Validate API Input

The API should validate data before inserting or updating database records.

For example:

  • Check required fields.
  • Validate email format.
  • Validate numeric IDs.
  • Check duplicate records.
  • Validate request data.

27. Security Basics

A REST API should be designed with security in mind.

  • Use prepared statements.
  • Validate user input.
  • Do not expose database credentials.
  • Use authentication for protected endpoints.
  • Return safe error messages.

Security will be covered in more detail in later lessons.

28. Complete Project Flow

MySQL Database
       ↓
PHP + PDO
       ↓
REST API
       ↓
Postman Testing
       ↓
React Native
       ↓
Mobile Application

Each layer has a specific responsibility in the application.

29. What We Will Build Next

In the upcoming lessons, we will build the project step by step.

  1. Create the project folder.
  2. Create the database.
  3. Create database tables.
  4. Create the PDO connection.
  5. Create the first REST API.
  6. Build GET APIs.
  7. Build POST APIs.
  8. Build PUT and PATCH APIs.
  9. Build DELETE APIs.
  10. Test everything with Postman.

30. PHP REST API Project Summary

In this project, PHP will provide REST API endpoints, MySQL will store student data, PDO will handle database communication, JSON will be used for data exchange, and Postman will be used to test the API. Later, the same API will be connected to a React Native mobile application.

React Native
      ↓
    REST API
      ↓
     PHP
      ↓
     PDO
      ↓
    MySQL

📌 Key Points

  • We are building a Student Management REST API.
  • PHP will be used for the backend.
  • MySQL will store application data.
  • PDO will be used for database communication.
  • JSON will be used for API data exchange.
  • GET can retrieve student data.
  • POST can create student data.
  • PUT and PATCH can update student data.
  • DELETE can remove student data.
  • Postman will be used to test the API.
  • React Native will later consume the API.
  • The API should validate input and return appropriate JSON responses.

🧠 Quick Quiz

Question: Which technology will be used to store student data in this PHP REST API project?