How to make Api in PHP

What is API ?

API stands for “Application Programming Interface”. It is a set of rules that allows one piece of software application to talk to another. Those “rules” can include create, read, update and delete operations.API is used for communicate data to server data.

Why do we need API?

In many applications, API is a need because this is the lightest way to create, read, update or delete information between different applications over the internet or HTTP protocol.

Where it is used?

API can be used by any application that can connect to the internet. If data from an application can be created, read, updated or deleted using another application, it usually means a API is used.

How to make API?

Step 1 : Create new Database with data in PhpMyAdmin.

Step 2: After creating MySQL database table, we will create db_connect.php file to make connection with MySQL database.

<?php

$host= "localhost";
$username = "root";
$password = "";
$dbname = "apidatabase";
$conn = mysqli_connect($host, $username, $password, $dbname);

?>

 

Step 3 :Now Create file to read products.
We will use this class to read data from the database. Open api folder. Create objects folder. Open objects folder. Create product.php file. Place the following code inside it.
<?php
class Product
{    
private $conn;    
private $table_name = "products";    

// object properties  
 
public $id;    
public $name;    
public $description;    
public $price;    
public $category_id;    
public $category_name;    
public $created;   

// constructor with $db as database connection  
  
public function __construct($db)
{        
$this->conn = $db; 

}

}
?>
Open api folder. Create product folder. Open product folder. Create read.php file. Place the following code inside it.
<?php

// required headers

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// database connection will be here

?>

 

  • Connect to database and products table

We need to use the getConnection() method of the Database class to get a database connection. We pass this connection to the Product class.

Replace of // database connection will be here comment of read.php file with the following code.

// include database and object files

include_once '../config/database.php';
include_once '../objects/product.php';

// instantiate database and product object

$database = new Database();
$db = $database->getConnection();

// initialize object

$product = new Product($db);

// read products will be here

  • Read products from the database
// query products

$stmt = $product->read();
$num = $stmt->rowCount();

// check if more than 0 record found

if($num>0)
{    

// products array    
$products_arr=array();    
$products_arr["records"]=array(); 
  
 // retrieve our table contents   

 while ($row = $stmt->fetch(PDO::FETCH_ASSOC))

{    
    
// extract row       
// this will make $row['name'] to        
// just $name only  
     
 extract($row);       
 $product_item=array( 
"id" => $id,            
"name" => $name,           
"description" => html_entity_decode($description),           
"price" => $price,            
"category_id" => $category_id,           
"category_name" => $category_name   
 );     
   
array_push($products_arr["records"], $product_item); 
   
}    

// set response code - 200 OK    

http_response_code(200);   

// show products data in json format  
 
echo json_encode($products_arr);

}

?>

 

Open objects folder. Open product.php file. Place the following code inside the Product class.
// read products

function read()

{    

// select all query    

$query = "SELECT c.name as category_name, p.id, p.name, p.description, p.price, p.category_id, p.created FROM  " . $this->table_name . " p   LEFT JOIN  categories c  ON p.category_id = c.id  ORDER BY   p.created DESC";    

// prepare query statement   
 
$stmt = $this->conn->prepare($query); 
  
// execute query  
 
$stmt->execute();    
return $stmt;

}

?>

 

Step 4 : Output

You need to use POSTMAN to test our API. Download your version of POSTMAN here.Launch POSTMAN. Enter the following as the request URL.

http://localhost/api/product/read.php Click the blue “Send” button. Output if there are product data.

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *