I am developing an API using core PHP. When I am returning data, all the fields data are returning as a string, but in some fields I have JSON type of data which is also coming as string
Example:
end_city: "mysore",
facilities: "{"Breakfast":false,"PickDrop":true,"Hotel":false,"Guide":true}"
Here, field facilities is having JSON, but getting this as a string in the response, want to get this as JSON only not string.
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT');
header('Access-Control-Allow-Headers: token, Content-Type');
// get database connection
require_once '../config/database.php';
// instantiate tours object
include_once '../objects/tours.php';
$db = new Database();
$tours = new Tours($db);
$tour = $tours->getTour($_REQUEST["t"]);
$num = $tour->num_rows;
if ($num > 0) {
while ($row = $tour->fetch_assoc()) {
echo json_encode($row);
}
// set response code - 200 OK
http_response_code(200);
} else {
// set response code - 404 Not found
http_response_code(404);
// tell the user no products found
echo json_encode(
array("message" => "No tour found.")
);
}
The exact scenario is, I have one form , after submit that form , I get the JSON data in some fields , after getting the all data I save that in mysql using core php , now I have to display that data on page using angualrjs . Now when I do get request , I get data in the JSON format and all the fields are in the type of string , even JSON fields I am getting as string which create problem.
http_response_code()suppose to be before anyecho()