Skip to content

How to receive json data in PHP?

// function to return JSON response, you can set cors, also give status & data via arguments
function json($status, $data) {
        $cors = "*";
        header("Access-Control-Allow-Origin: $cors");
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($data);

// example
json(200, [
	"status" => 200,
  	"message" => "action completed successfully",
  	"id" => 44236
See also  Python code snippet - How to only print final iteration of a for loop pyhton?

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.