![]() |
Returning JSON Data consists of converting PHP data structures like arrays or objects into a JSON format. In this article, we will explore two different approaches to Return JSON from a PHP Script. Below are the approaches to Return JSON from a PHP Script: Table of Content Using json_encode()In this approach, we are using json_encode() to convert a PHP associative array ($jsonData) containing organization, founder, and employee data into JSON format. The header(‘Content-Type: application/json’) sets the response header to indicate that the content being sent is JSON data. Example: The below example uses json_encode() to Return JSON from a PHP Script.
Output: { Using file_get_contents()In this approach, we are using file_get_contents() to fetch JSON data from the specified URL ($jsonPlaceholderUrl), which in this case is the JSONPlaceholder API. The header(‘Content-Type: application/json’) sets the response header to indicate that the content being sent is JSON data, and echo $response outputs the fetched JSON data. Example: The below example uses file_get_contents() to Return JSON from a PHP Script.
Output: { GIF: |
Reffered: https://www.geeksforgeeks.org
PHP |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |