O'Reilly logo

Building a Web Application with PHP and MariaDB: A Reference Guide by Sai Srinivas Sriparasa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating JSON feeds

In the last section, we used PHP's SimplXMLElement class to generate our XML feeds. In this section, we will use PHP's json_encode function to generate our JSON feed. Building a JSON feed is very simple when compared to building the XML feed. JSON is a very popular data exchange format and is considered lightweight when compared to XML, as shown in the following code snippet, present in the controllers/api.php file:

private function _generateJSON($root, $data){
  header("HTTP/1.1 200 OK");
  header("Content-Type: application/json");
  echo json_encode(array($root=>$data));
}

In this snippet, we begin by building our _generateJSON method that will expect the name of the endpoint and the data that was fetched by the get action. Now, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required