This RESTful API allows you to manage recipes and their ratings using PHP and MongoDB. The API supports basic CRUD operations and allows for searching and rating recipes.
Make sure to replace the MongoDB connection and setup information with your actual credentials and details. This README should provide enough documentation for others to easily use and understand my API.
First create MONGO_USER
& MONGO_PASS
from your mongodb atlas Account
Then add it in your server's Secrets/.env or add it in $mongoUri
string of /index.php (e.g.,
$mongoUri = "mongodb+srv://user123:pass123@cluster0.i2re1dg.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0";
,
$mongoUri = "mongodb://user123:pass123@localhost:27017/mydatabase";
).
$ docker build -t my-image .
$ docker run -p 4000:80 my-image
Then follow the 6th step of Installation
- PHP 7.x or higher
- MongoDB server or MongoDB Atlas
- Composer (for dependencies)
-
Clone the repository:
$ git clone https://github.com/cnvrt/api-recipes.git $ cd recipe-api
-
Install dependencies:
$ composer install
-
Set up your MongoDB connection:
Create an
.env
file and add your MongoDB credentials:MONGO_USER=your_username MONGO_PASS=your_password
-
Run the API:
You can use PHP's built-in server for local testing:
$ php -S localhost:8000
-
If the PHP
mongodb
extension is not enabled:For Windows:
$ php -i | findstr /C:"extension_dir" extension_dir => \xampp\php\ext => \xampp\php\ext
- Unzip the downloaded file and move
php_mongodb.dll
to the directory received from the command above (e.g.,C:\xampp\php\ext\
).
$ php --ini Configuration File (php.ini) Path: Loaded Configuration File: C:\xampp\php\php.ini $ echo extension=php_mongodb.dll >> C:/xampp/php/php.ini
For Linux:
$ apt-get update $ pecl install mongodb $ php --ini Configuration File (php.ini) Path: /usr/local/php/8.2.13/ini Loaded Configuration File: /usr/local/php/8.2.13/ini/php.ini $ echo extension=mongodb.so >> /usr/local/php/8.2.13/ini/php.ini
- Unzip the downloaded file and move
-
If you are using MongoDB Atlas, whitelist your server's IP address:
-
Open
http://localhost:8000/ip.php
and copy the IP address from the page. -
Paste the IP address into the Network Access section of your MongoDB Atlas configuration.
-
Returns an empty body.
GET / HTTP/1.1
Host: localhost:8000
Retrieves all the recipes stored in the MongoDB database.
GET /recipes HTTP/1.1
Host: localhost:8000
[
{
"_id": "64f1f9cbe25b43d3446789bc",
"name": "Spaghetti Carbonara",
"ingredients": ["spaghetti", "eggs", "bacon", "cheese"],
"instructions": "Boil pasta. Fry bacon. Mix eggs with cheese. Combine all."
},
{
"_id": "64f1f9d2e25b43d3446789bd",
"name": "Chicken Alfredo",
"ingredients": ["chicken", "pasta", "cream", "cheese"],
"instructions": "Cook pasta and chicken. Combine with cream and cheese."
}
]
Adds a new recipe to the database.
POST /recipes HTTP/1.1
Host: localhost:8000
Content-Type: application/json
{
"name": "Pancakes",
"ingredients": ["flour", "milk", "eggs"],
"instructions": "Mix ingredients. Cook on medium heat."
}
{
"message": "Recipe created",
"_id": "64f1f9cbe25b43d3446789bf",
"data": {
"name": "Pancakes",
"ingredients": ["flour", "milk", "eggs"],
"instructions": "Mix ingredients. Cook on medium heat."
}
}
Fetches a single recipe by its id.
GET /recipes/64f1f9cbe25b43d3446789bf HTTP/1.1
Host: localhost:8000
{
"_id": "64f1f9cbe25b43d3446789bf",
"name": "Pancakes",
"ingredients": ["flour", "milk", "eggs"],
"instructions": "Mix ingredients. Cook on medium heat."
}
Updates an existing recipe.
PUT /recipes/64f1f9cbe25b43d3446789bf HTTP/1.1
Host: localhost:8000
Content-Type: application/json
{
"name": "Blueberry Pancakes",
"ingredients": ["flour", "milk", "eggs", "blueberries"]
}
{
"message": "Recipe updated"
}
Deletes a recipe by its id.
DELETE /recipes/64f1f9cbe25b43d3446789bf HTTP/1.1
Host: localhost:8000
{
"message": "Recipe deleted"
}
Adds a rating to a recipe.
POST /recipes/64f1f9cbe25b43d3446789bf/rating HTTP/1.1
Host: localhost:8000
Content-Type: application/json
{
"rating": 5,
"comment": "Delicious and easy to make!"
}
{
"message": "Rating added",
"data": {
"recipe_id": "64f1f9cbe25b43d3446789bf",
"rating": 5,
"comment": "Delicious and easy to make!"
}
}
Search for recipes by name. The search is case-insensitive.
GET /search?q=pancakes HTTP/1.1
Host: localhost:8000
[
{
"_id": "64f1f9cbe25b43d3446789bf",
"name": "Pancakes",
"ingredients": ["flour", "milk", "eggs"],
"instructions": "Mix ingredients. Cook on medium heat."
}
]
$ curl -X POST http://localhost:8000/recipes -H "Content-Type: application/json" -d '{"name": "Pancakes", "ingredients": ["flour", "milk", "eggs"], "instructions": "Mix ingredients. Cook on medium heat."}'
$ curl http://localhost:8000/recipes
$ curl http://localhost:8000/search?q=pancakes
Make sure to replace the MongoDB connection and setup information with your actual credentials and details. This README should provide enough documentation for others to easily use and understand my API.
This project is open-source and available under the MIT License.