1

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

3
  • What do you mean about the Response code? Commented Aug 18, 2018 at 10:14
  • Response code means when we hit on the server it will return 200, 403, 400 or it failure or success @KhoaTruongDinh Commented Aug 18, 2018 at 10:16
  • or means status code while it is failure or success @KhoaTruongDinh Commented Aug 18, 2018 at 10:19

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.")
        );
    }
?>

4 Comments

is header("HTTP/1.1 200 OK"); gives me success or failure message @Lajos Arpad
@omkara it seems that you are mixing up HTTP response with API response. HTTP response is about the request successfully sent and handled by the server. It should not be mixed up with the internal logic of your API. You should always send a success message as HTTP status if your server-side was able to handle the request and if you have a logical error, like no products found, even then the request was successfully sent, you will need to handle the response accordingly. You might want to add an ErrorCode to your response, which would be meaningful about any logical errors.
one more thing @Lajos Arpad I want to create hyperlink inside array() i.e array( "image_name"=>$image_name,"image_url"=>"http://localhost/android/images/image1.jpg" ) somthing like that but it response me wrong like image_url=>http:/\/\localhost/\android/\images/\image1.jpg
@omkara you will need to escape special characters. In our case it would be sufficient to use %2F instead of / (as %2F is its HTML code), but it would not hurt to encode the characters using htmlentities. Read more here: php.net/manual/ro/function.htmlentities.php. If my answer solved your problem, you might consider accepting it as the correct solution.
0

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

1 Comment

This won't change the final status

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.