I have an API route that outputs records from a database as JSON. I would also like to output an HTTP status code.
I have tried echoing (as shown below) the HTTP status code & message before the return like above which functions how I would like, however, it is not valid JSON since I end up with two side by side JSON objects.
Here is a simplified view of my code:
private function json_books() {
$query = "SELECT title, description FROM books";
$params= [];
// creates and prints the HTTP status code message
$msg = array("status" => "200", "message"=>"OK");
echo json_encode($msg);
//returns records from the database as JSON object
return ($this->recordset->getJSONRecordSet($query, $params));
}