PHP : REST API – Introduction
An API (Application Programming Interface) lets two software systems talk to each other. A REST API does this over the internet using normal HTTP requests, and it usually sends and receives data in JSON format. In this tutorial you will learn how PHP can read data from an API, and how it can create, update and delete data using an API.
Why do we use APIs?
- A weather app gets live weather data from a weather service.
- A website shows payments through a payment gateway, like Razorpay or PayPal.
- The same data is used by a website and by a mobile app, both call the same API.
- A website shows your social media posts, by reading them from the API of that platform.
What is REST?
REST (REpresentational State Transfer) is a style for building web services. Every thing (like a user or a post) has its own URL, called an endpoint, and the HTTP method tells what to do with it.
| HTTP method | Use | Example |
|---|---|---|
| GET | Read data | GET /users/1 gets the user with id 1 |
| POST | Create new data | POST /users creates a new user |
| PUT | Replace or update data | PUT /users/1 updates the user 1 |
| PATCH | Update only some fields | PATCH /users/1 |
| DELETE | Remove data | DELETE /users/1 deletes the user 1 |
Common HTTP status codes
| Code | Meaning |
|---|---|
| 200 | OK, the request worked |
| 201 | Created, a new record was made |
| 204 | Success, with no content to return |
| 400 | Bad request, your data is wrong |
| 401 / 403 | Not logged in / not allowed |
| 404 | Not found |
| 500 | Server error |
What is JSON?
JSON (JavaScript Object Notation) is the most used format to send data between applications. Objects are written in curly brackets { } and lists in square brackets [ ].
{
"id": 1,
"name": "Robin",
"skills": ["PHP", "MySQL"]
}
PHP has two functions for JSON: json_decode() turns JSON text into a PHP array or object, and json_encode() does the opposite.
<?php
$json = '{"id":1,"name":"Robin","skills":["PHP","MySQL"]}';
$data = json_decode($json, true); // true = give an array
echo $data["name"] . " knows " . $data["skills"][0] . "<br>";
echo json_encode(["id" => 1, "name" => "Robin"]);
?>
Output:
Robin knows PHP
{“id”:1,”name”:”Robin”}
Ways to call an API from PHP
| Method | When to use | Chapter |
|---|---|---|
| file_get_contents() | Simple GET requests | file_get_contents() |
| cURL | Full control: all methods, headers, errors | GET, POST, PUT, DELETE |
| Guzzle library | Big projects (installed with Composer) | Not covered here |
Practice API used in this tutorial
To practice we use JSONPlaceholder, a free fake API for testing, at https://jsonplaceholder.typicode.com. It has endpoints like /users and /posts. It accepts POST, PUT and DELETE requests and gives a correct response, but it does not really save any change, so you can test without any risk. If the site is not available in future, you can use any other test API with the same code idea.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |