Chrome Extension

Return JSON from a PHP script

To return JSON data from you PHP script you can use json_encode() function. It will convert PHP array to JSON string.
<?php
    header('Content-Type: application/json'); 

    //First Examaple
    $json_data = array("key" => "value");
    echo json_encode($json_data);

    // More Complex example
    $php_arr = [
        [ "id" => 1, "name" => "Math" ],
        [ "id" => 1, "name" => "physics" ],
        [ "id" => 1, "name" => "Chemistry" ]
    ];
    echo json_encode($php_arr);
?>
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

To return JSON from a PHP script you can set its header to 'Content-Type: application/json'.

This will return data in JSON format as your API response.


Was this helpful?
0 Comments