Hello! Welcome to the next stage of your journey toward PHP mastery.
In our last module, we built a complete and secure user authentication system. You learned how to register users, log them in, and protect pages by checking their session status. That was a critical milestone in building self-contained web applications.
Now, we're shifting gears to a topic that is essential for modern web development: building applications that can communicate with other systems. This lesson kicks off our module on RESTful APIs. Today, we will focus on the foundational theory: explaining the principles of REST and the use of HTTP methods like GET, POST, PUT, and DELETE. Understanding this "why" and "what" is the first step before we start writing any API code.
1. From Web Pages to APIs: A New Way to Communicate
So far, you've used PHP to generate HTML pages that are sent to a browser. An API (Application Programming Interface) works on a similar client-server model, but instead of sending visual HTML, the server sends raw data, usually in a structured format like JSON. This allows different applications (like a mobile app, a JavaScript front-end, or even another server) to use your PHP backend's logic and data.
REST (REpresentational State Transfer) is the most popular architectural style for designing these APIs. It's not a strict protocol but a set of guiding principles that make APIs scalable, stateless, and easy to use.
To get a quick overview, let's watch the first couple of minutes of this video from ByteByteGo.
What Is REST API? Examples And How To Use It: Crash Course System Design #3
This video provides a concise introduction to what a REST API is, why it's used, and how it organizes data into 'resources'.
Watch from the beginning until the timestamp 01:27. Focus on the definition of REST and the concept of organizing your application's data into noun-based 'resources' (e.g., /products instead of /getAllProducts).
The key idea here is that a REST API exposes resources. A resource is any piece of data in your system: a user, a blog post, a product, etc. Each resource is identified by a unique URL (or URI, Uniform Resource Identifier). For example:
- A collection of all users might be at
/users. - A specific user might be at
/users/123.
But how do we tell the server what we want to do with that resource? That's where HTTP methods come in.
2. The Language of REST: HTTP Methods
You are already familiar with GET and POST from handling web forms. REST leverages these and other standard HTTP methods to perform actions on resources. This maps very cleanly to the CRUD (Create, Read, Update, Delete) operations you know from database management.

Here's the standard mapping:
- GET: Read a resource.
- POST: Create a new resource.
- PUT: Update or replace an existing resource.
- DELETE: Delete a resource.
Let's dive into the specifics of each method. The following article from restfulapi.net provides a clear and detailed explanation.
HTTP Methods - REST API Tutorial
This article is an excellent reference for understanding how each HTTP method should be used in a REST context. It also introduces two very important professional concepts: safety and idempotency.
Please read the following sections from the article: Section 1: HTTP GET: Note its purpose (retrieval) and characteristics (safe, idempotent). Section 2: HTTP POST: Note its purpose (creation) and characteristics (not safe, not idempotent). Section 3: HTTP PUT: Note its purpose (update/replace). Section 4: HTTP DELETE: Note its purpose (deletion) and characteristics (idempotent). Section 6: Summary of HTTP Methods: Study the summary table. It's a fantastic cheat sheet. Sections 7.1 (Safe Methods) & 7.2 (Idempotent Methods): Read these glossary definitions carefully.
Key Concepts: Safety and Idempotency
Let's crystallize those two important terms from the article, as they are crucial for professional development and relate to your interest in security and system robustness.
-
Safe Method: A method is "safe" if it doesn't change the state (the data) on the server.
GETis a safe method. You can request a user's data a thousand times; the user's data itself won't change because of it. Search engine crawlers and caching systems rely on this to work without fear of accidentally modifying data. -
Idempotent Method: A method is "idempotent" if making the same request multiple times has the same effect as making it once.
GET,PUT, andDELETEare idempotent. If you send a request toDELETE /users/123three times, the first call deletes the user, and the next two do nothing (or return a "Not Found" error). The end result is the same: the user is gone.POSTis not idempotent. If you send aPOSTrequest to/usersthree times with the same user data, you will create three new, separate users. This is an important distinction to prevent accidental data creation.
Here's a summary table based on the one in the article you read:
| HTTP Method | CRUD Operation | Example URI | Idempotent? | Safe? |
|---|---|---|---|---|
| GET | Read | /users or /users/123 |
Yes | Yes |
| POST | Create | /users |
No | No |
| PUT | Update/Replace | /users/123 |
Yes | No |
| DELETE | Delete | /users/123 |
Yes | No |
3. The Guiding Principles of REST
While using HTTP methods for CRUD is a core part of REST, it's not the whole story. REST is an architectural style defined by several principles. An API that follows these principles is called "RESTful".
The following video provides an excellent breakdown, clearly distinguishing the rules of the HTTP protocol from the architectural principles of REST.
What are HTTP REST APIs? | GET, POST, PUT, PATCH, DELETE
This video will help you understand that REST is more than just CRUD over HTTP. It explains the formal principles that make a system truly 'RESTful'.
Watch the section titled 'What exactly is REST?' from 00:10:07 to 00:15:17. The video author, Roy Fielding, who coined the term REST, outlines five guiding principles. Pay attention to each one.
To reinforce those ideas, here is a summary of the core REST principles from the AWS documentation.
This article from Amazon Web Services provides a solid, text-based explanation of the key principles of the REST architectural style.
Read the section titled 'What is REST?' and pay close attention to the descriptions of the principles: Uniform interface, Statelessness, Layered system, and Cacheability.
Let's briefly summarize the most critical principles for our purposes:
- Client-Server: The client (who asks for data) and the server (who provides it) are separate. This allows them to be developed and scaled independently. Your PHP backend can serve a web app, a mobile app, and a desktop app, all at the same time.
- Statelessness: This is a big one. Each request from the client to the server must contain all the information the server needs to understand and process the request. The server does not store any client context between requests. Unlike the session-based web application we just built, a RESTful server doesn't rely on
$_SESSION. Authentication is typically handled by sending a token with every single request. - Uniform Interface: This is the principle that enforces consistency. It includes using nouns for resource URIs, using HTTP verbs for actions, and providing self-descriptive messages. It's why
GET /usersis the standard way to fetch users in any RESTful API.
The diagram below shows a simplified view of how these pieces fit together. A client sends an HTTP request (e.g., GET /users/123), which the web service processes, possibly by interacting with a database, before returning a response (typically JSON data).

Conclusion
In this lesson, we've laid the theoretical groundwork for building APIs. You've learned that REST is not a technology, but a set of design principles for creating scalable and easy-to-use web services.
Key Takeaways:
- An API allows applications to communicate by exchanging data, not HTML.
- REST is an architectural style for designing APIs based on resources identified by URIs.
- Standard HTTP methods are used to perform CRUD operations:
GETto read.POSTto create.PUTto update/replace.DELETEto remove.
- Key principles of REST include client-server separation, statelessness, and a uniform interface.
- Understanding method characteristics like safety and idempotency is crucial for building robust systems.
Preview of the Next Lesson:
Theory is great, but now it's time to write some code. In our next lesson, we will take the first practical step: handling incoming API requests and routing them based on the URL and HTTP method. We'll build a basic router in PHP that can look at a request like GET /posts/5 and know that the client wants to read the post with ID 5.