Welcome to the exciting world of PHP! In this tutorial, we'll learn how to create a REST API using PHP, perfect for beginners and intermediates. Let's dive in!
REST (Representational State Transfer) API is a set of rules that allows different software systems to communicate with each other over the internet. It's widely used for building web services for mobile apps, web applications, and IoT devices.
PHP is a popular server-side scripting language used in creating dynamic web pages. It's open-source, easy to learn, and has extensive community support, making it a great choice for building REST APIs.
Before we begin, ensure you have the following installed:
For this tutorial, we'll use the built-in PHP web server for simplicity.
We'll create a simple API to manage a list of books.
// Create a file named books.phpLet's define a Book class to represent a book object.
// books.php
class Book {
public $title;
public $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
}Now, let's create methods for the following API endpoints:
GET /books: Retrieve all booksPOST /books: Add a new bookGET /books/:id: Retrieve a specific bookPUT /books/:id: Update a specific bookDELETE /books/:id: Delete a specific book// books.php
// ... (Book class definition)
$books = array();
// Add some sample books
$books[] = new Book("Book 1", "Author 1");
$books[] = new Book("Book 2", "Author 2");
function getBooks() {
global $books;
return $books;
}// books.php
// ... (getBooks function)
function addBook($title, $author) {
global $books;
$books[] = new Book($title, $author);
}// books.php
// ... (getBooks and addBook functions)
function getBook($id) {
global $books;
foreach ($books as $book) {
if ($book->id === $id) {
return $book;
}
}
return null;
}// books.php
// ... (getBook function)
function updateBook($id, $title, $author) {
global $books;
foreach ($books as $key => $book) {
if ($book->id === $id) {
$books[$key] = new Book($title, $author);
break;
}
}
}// books.php
// ... (updateBook function)
function deleteBook($id) {
global $books;
foreach ($books as $key => $book) {
if ($book->id === $id) {
unset($books[$key]);
break;
}
}
}Now, we'll handle the incoming requests using PHP's built-in $_SERVER superglobal array.
// books.php
// ... (all functions above)
if ($_SERVER["REQUEST_METHOD"] === "GET") {
// Handle GET requests
if (isset($_GET["id"])) {
$book = getBook($_GET["id"]);
echo json_encode($book ? $book : array("error" => "Book not found"));
} else {
echo json_encode(getBooks());
}
} elseif ($_SERVER["REQUEST_METHOD"] === "POST") {
// Handle POST requests
$title = $_POST["title"];
$author = $_POST["author"];
addBook($title, $author);
echo json_encode(array("message" => "Book added successfully"));
} elseif ($_SERVER["REQUEST_METHOD"] === "PUT") {
// Handle PUT requests
$id = intval($_GET["id"]);
$title = $_POST["title"];
$author = $_POST["author"];
updateBook($id, $title, $author);
echo json_encode(array("message" => "Book updated successfully"));
} elseif ($_SERVER["REQUEST_METHOD"] === "DELETE") {
// Handle DELETE requests
$id = intval($_GET["id"]);
deleteBook($id);
echo json_encode(array("message" => "Book deleted successfully"));
} else {
echo json_encode(array("error" => "Invalid request method"));
}Now that we have our API ready, let's test it using curl or a tool like Postman.
// Using curl
curl -X GET http://localhost/books
curl -X POST -d 'title=Book 3&author=Author 3' http://localhost/books
curl -X GET http://localhost/books/1Now you have a basic understanding of creating a REST API using PHP. You can further improve this API by:
Why is PHP a good choice for creating REST APIs?