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');
        http_response_code($status);
        echo json_encode($data);
    }

// example
json(200, [
	"status" => 200,
  	"message" => "action completed successfully",
  	"id" => 44236
]);
See also  PHP code snippet - codeigniter How to know update failed?

Leave a Reply

Your email address will not be published.

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