omkara
omkara

Reputation: 982

How to set Response status code and message while using REST api in php?

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

Upvotes: 1

Views: 7733

Answers (3)

shekhar chander
shekhar chander

Reputation: 618

header("HTTP/1.1 200 OK"); // This line will set the server response to 200

header("HTTP/1.1 404 ERROR"); // This will throw a 404 error. You can change this code to any corresponding code

Upvotes: 1

vivek
vivek

Reputation: 352

I think it's solve your problem

echo json_encode( array("status"="404","message" => "No products found.") );

Here status is your response code. you can modified this response code according to you

Upvotes: 0

Lajos Arpad
Lajos Arpad

Reputation: 77002

This is how you set an HTTP status of 200:

header("HTTP/1.1 200 OK");

apply this in your code:

<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("HTTP/1.1 200 OK");
    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.")
        );
    }
?>

Upvotes: 0

Related Questions