product.php
<?php
class Product
{
private $conn;
private $table_name = "category";
public $image_name;
public $file_s;
public function __construct($db){
$this->conn = $db;
}
function read()
{
$query = "SELECT * FROM ".$this->table_name." order by category_name";
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt;
}
}
read.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include_once 'dbase.php';
include_once 'product.php';
$database = new Database();
$db = $database->getConnection();
$product = new Product($db);
$stmt = $product->read();
$num = $stmt->rowCount();
if($num>0)
{
$products_arr=array();
$products_arr["data"]=array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$product_item=array(
"image_name" => $image_name,
"image_url" => $file_s
);
array_push($products_arr["data"], $product_item);
}
echo json_encode($products_arr);
}
else{
echo json_encode(
array("message" => "No products found.")
);
}
?>
I have create a simple REST api which work perfectly but now the problem is that I don't have an any idea of Response code and Response message that how to add with REST api in php?
Thank You