Creating a Simple Library REST API with PHP

 Step 1: Project Setup

Begin by creating a project folder and navigating into it:

mkdir library_api
cd library_api

Step 2: File Structure

Create three files in your project folder:

index.php: The main file for handling API requests.

database.php: File for database connection.

books.php: File for our simple "Book" model.

Step 3: Database Connection

In database.php, establish a connection to a MySQL database. Replace your_database_name, your_username, and your_password with your actual database credentials.

Step 4: Book Model

In books.php, define a simple Book model with properties for a book:

<?php
class Book {
    public $id;
    public $title;
    public $author;
    public $genre;
    public function __construct($id, $title, $author, $genre) {
        $this->id = $id;
        $this->title = $title;
        $this->author = $author;
        $this->genre = $genre;
    }
}
?>

Step 5: Handling API Requests in index.php

In index.php, handle different API requests for CRUD operations without using a switch case:


<?php
class Book {
    public $id;
    public $title;
    public $author;
    public $genre;

    public function __construct($id, $title, $author, $genre) {
        $this->id = $id;
        $this->title = $title;
        $this->author = $author;
        $this->genre = $genre;
    }
}
?>
Step 5: Handling API Requests in index.php
In index.php, handle different API requests for CRUD operations without using a switch case:

<?php
header("Content-Type: application/json");

include_once 'database.php';
include_once 'books.php';

$request_method = $_SERVER["REQUEST_METHOD"];

// Handle GET request
if ($request_method === 'GET') {
    // Read books
    $books = array(
        new Book(1, "To Kill a Mockingbird", "Harper Lee", "Fiction"),
        new Book(2, "1984", "George Orwell", "Dystopian")
    );
    echo json_encode($books);
}

// Handle POST request
if ($request_method === 'POST') {
    // Create a book
    $data = json_decode(file_get_contents("php://input"));
    $new_book = new Book(3, $data->title, $data->author, $data->genre);
    echo json_encode(array("message" => "Book created.", "book" => $new_book));
}

// Handle PUT request
if ($request_method === 'PUT') {
    // Update a book
    $data = json_decode(file_get_contents("php://input"));
    $updated_book = new Book(1, $data->title, $data->author, $data->genre);
    echo json_encode(array("message" => "Book updated.", "book" => $updated_book));
}

// Handle DELETE request
if ($request_method === 'DELETE') {
    // Delete a book
    $data = json_decode(file_get_contents("php://input"));
    echo json_encode(array("message" => "Book deleted.", "book_id" => $data->id));
}
?>

Once you've written the code for your API, you can test it using tools like Postman or curl. Make requests to the endpoints you've defined, and ensure that the API behaves as expected.


You've successfully created a simple RESTful API for a library using PHP

Comments

Popular posts from this blog

Installing LAMP

Building a Database with PHP, PDO, and MySQL on Linux