This Project is a simple PHP REST API made it with no framework. The purpose of this project is learn how to create a PHP API without implement a framework and create your own functionalities using Object Oriented Programming.
To read environment variables in file .env I used PHP dotenv.
Installation via Composer:
$ composer require vlucas/phpdotenv
This API allows you manage a bookstore. You can create, read, update, delete and get information about a single book, author or category.
http://localhost/php-rest-api/api/books
{
"status": 200,
"data": [
{
"id": 22,
"title": "Poster Girl",
"description": null,
"author_id": 6,
"author_name": "Veronica Roth",
"category_id": 2,
"category_name": "Adventure"
},
{
"id": 21,
"title": "Chosen ones",
"description": "Fifteen years ago, five ordinary teenagers were singled out by a prophecy to take down an impossibly powerful entity wreaking havoc across North America. He was known as the Dark One, and his weapon of choice catastrophic events known as Drains leveled cities and claimed thousands of lives. Chosen Ones, as the teens were known, gave everything they had to defeat him.",
"author_id": 6,
"author_name": "Veronica Roth",
"category_id": 2,
"category_name": "Adventure"
},
...
...
]
}
http://localhost/php-rest-api/api/authors
http://localhost/php-rest-api/api/categories
http://localhost/php-rest-api/api/book?id=1
{
"status": 200,
"data": {
"id": 1,
"title": "Pride and Prejudice",
"description": "Pride and Prejudice tells the story of Mr and Mrs Bennet's five unmarried daughters after the rich and eligible Mr Bingley and his status-conscious friend, Mr Darcy, have moved into their neighbourhood.",
"author_id": 2,
"author_name": "Jane Austen",
"category_id": 1,
"category_name": "Romantic novel"
}
}
http://localhost/php-rest-api/api/author?id=1
http://localhost/php-rest-api/api/category?id=1
http://localhost/php-rest-api/api/book/create
// description can be null
{
"title": "Christmas Eve at Friday Harbor",
"author_id": 11,
"category_id": 1
}
http://localhost/php-rest-api/api/author/create
{
"author_name": "Julia Quinn"
}
http://localhost/php-rest-api/api/category/create
{
"name": "Comedy"
}
http://localhost/php-rest-api/api/book?id=5
//Send Json with fields you need to update
{
"title": "THe Fifth Wave"
}
http://localhost/php-rest-api/api/author?id=2
//Send Json with field you need to update, in this case "author_name"
http://localhost/php-rest-api/api/category?id=3
//Send Json with field you need to update, in this case "name"
http://localhost/php-rest-api/api/book?id=25
{
"status": 200,
"data": "The book was delete succesfully"
}
http://localhost/php-rest-api/api/author?id=12
http://localhost/php-rest-api/api/category?id=7
// Bad request
{
"status": 500,
"message": "Bad request, you must send Id parameter."
}
// Invalid JSON
{
"status": 500,
"message": "Invalid json."
}
// Not found
{
"status": 404,
"message": "Book not found."
}