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

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





What do you mean about the Response code?
– Khoa TruongDinh
yesterday





Response code means when we hit on the server it will return 200, 403, 400 or it failure or success @KhoaTruongDinh
– omkara
yesterday





or means status code while it is failure or success @KhoaTruongDinh
– omkara
yesterday




2 Answers
2



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

?>





is header("HTTP/1.1 200 OK"); gives me success or failure message @Lajos Arpad
– omkara
yesterday


header("HTTP/1.1 200 OK")





@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.
– Lajos Arpad
yesterday





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
yesterday


array()


array( "image_name"=>$image_name,"image_url"=>"http://localhost/android/images/image1.jpg" )


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.
– Lajos Arpad
yesterday



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Help:Category

How can temperature be calculated given relative humidity and dew point?

I have a recursive function to validate tree graph and need a return condition