BUILDING A SIMPLE REST API FOR INVENTORY MANAGEMENT WITH PHP
The method is the type of request you send to the server. The four main resource methods that are associated with REST APIs are:
- GET: This method allows for the server to find the data you requested and sends it back to you.
- PUT: If you perform the ‘PUT’ request, then the server will update an entry in the database.
- POST: This method permits the server to create a new entry in the database.
- DELETE: This method allows the server to delete an entry in the database.
You can use the below reference link to Set Up your PHP Environment.
https://chiragvalvai.blogspot.com/2023/12/steps-to-install-process-of-php-with.html
STEP 2 : WRITE PHP CODE FOR API
<?php
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] ===
'GET' && isset($_GET['items'])) {
<?php
include 'db.php';
if
($_SERVER['REQUEST_METHOD'] === 'GET') {
$sql
= "SELECT * FROM items";
$result
= $conn->query($sql);
if
($result->num_rows > 0) {
$items
= array();
while
($row = $result->fetch_assoc()) {
$items[]
= $row;
}
echo
json_encode($items);
}
else {
echo
"No items found.";
}
}
?>
}
if ($_SERVER['REQUEST_METHOD'] === 'GET'
&& isset($_GET['items']) && isset($_GET['id'])) {
$id =
$_GET['id'];
<?php
include 'db.php';
if
($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['items'])
&& isset($_GET['id'])) {
$item_id
= $_GET['id'];
$sql
= "SELECT * FROM items WHERE id = $item_id";
$result
= $conn->query($sql);
if
($result->num_rows > 0) {
//
Fetch the item data
$item
= $result->fetch_assoc();
header('Content-Type:
application/json');
echo
json_encode($item);
}
else {
header("HTTP/1.0
404 Not Found");
echo
"Item not found.";
}
}
?>
}
if ($_SERVER['REQUEST_METHOD'] ===
'POST' && isset($_POST['items'])) {
<?php
include 'db.php';
if
($_SERVER['REQUEST_METHOD'] === 'POST') {
$name
= $_POST['name'];
$description
= $_POST['description'];
$price
= $_POST['price'];
$quantity
= $_POST['quantity'];
$category
= $_POST['category'];
$sql
= "INSERT INTO items (name, description, price, quantity, category) VALUES
('$name', '$description', $price, '$quantity', '$category')";
if
($conn->query($sql) === TRUE) {
echo
"Item added successfully.";
}
else {
echo
"Error: " . $conn->error;
}
}
?>
}
if ($_SERVER['REQUEST_METHOD'] ===
'PUT' && isset($_PUT['items']) && isset($_PUT['id'])) {
$id =
$_PUT['id'];
<?php
include 'db.php';
if
($_SERVER['REQUEST_METHOD'] === 'PUT') {
parse_str(file_get_contents("php://input"),
$_PUT);
$id
= $_PUT['id'];
$name
= $_PUT['name'];
$description
= $_PUT['description'];
$price
= $_PUT['price'];
$quantity
= $_PUT['quantity'];
$category
= $_PUT['category'];
$sql
= "UPDATE items SET name='$name', description='$description',
price=$price, quantity='$quantity', category='$category' WHERE id=$id";
if
($conn->query($sql) === TRUE) {
echo
"Item updated successfully.";
}
else {
echo
"Error: " . $conn->error;
}
}
?>
}
if ($_SERVER['REQUEST_METHOD'] ===
'DELETE' && isset($_DELETE['items']) && isset($_DELETE['id']))
{
$id =
$_DELETE['id'];
<?php
include 'db.php';
if
($_SERVER['REQUEST_METHOD'] === 'DELETE') {
parse_str(file_get_contents("php://input"),
$_DELETE);
$id
= $_DELETE['id'];
$sql
= "DELETE FROM items WHERE id=$id";
if
($conn->query($sql) === TRUE) {
echo
"Item deleted successfully.";
}
else {
echo
"Error: " . $conn->error;
}
}
?>
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.
CONCLUSION :
Creating a REST API for inventory management is a valuable skill in today's tech-savvy world. It allows businesses to automate and streamline their operations while providing a modern and efficient user experience. With PHP and a well-designed database, you can create a powerful inventory management system that suits your specific needs.
Remember that this tutorial provides a basic structure for your REST API, and you may need to tailor it to your project's requirements. Additionally, it's important to secure your API by implementing authentication and authorization mechanisms for production use.
Building a RESTful API can be a rewarding endeavor, and it opens up a world of possibilities for connecting your inventory system to various applications and platforms. Good luck with your API development journey!
Comments
Post a Comment