The Wayback Machine - https://web.archive.org/web/20240715050539/https://www.geeksforgeeks.org/how-to-receive-json-post-with-php/
Open In App

How to receive JSON POST with PHP ?

Last Updated : 06 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to retrieve the JSON POST with PHP, & will also see their implementation through the examples. First, we will look for the below 3 features:

  • php://input: This is a read-only stream that allows us to read raw data from the request body. It returns all the raw data after the HTTP headers of the request, regardless of the content type.
  • file_get_contents() function: This function in PHP is used to read a file into a string.
  • json_decode() function: This function takes a JSON string and converts it into a PHP variable that may be an array or an object.

It is known that all of the post data can be received in a PHP script using the $_POST[] global variable. But this fails in the case when we want to receive JSON string as post data. To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

Handling JSON POST requests:

// Takes raw data from the request
$json = file_get_contents('php://input');

// Converts it into a PHP object
$data = json_decode($json);

Example 1: This example uses the json_decode() function that is used to decode a JSON string.

PHP




<?php
  $json = '["geeks", "for", "geeks"]';
  $data = json_decode($json);
  echo $data[0];
?>


Output: 

geeks

 

Example 2: This example uses the json_decode() function that is used to decode a JSON string.

PHP




<?php
  $json = '{
      "title": "PHP",
      "site": "GeeksforGeeks"
  }';
  $data = json_decode($json);
  echo $data->title;
  echo "\n";
  echo $data->site;
?>


Output: 

PHP
GeeksforGeeks

 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Previous Article
Next Article

Similar Reads

How to receive post parameter in Express.js ?
Express is a small framework that sits on top of Node.js’s web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing; it adds helpful utilities to Node.js’s HTTP objects; it facilitates the rendering of dynamic HTTP objects. Express.js prov
3 min read
How to Receive JSON Data at Server Side ?
JavaScript Object Notation (JSON) is a data transferring format used to send data to or from the server. It is commonly utilized in API integration due to its benefits and simplicity. In this example, we will utilize XML HttpRequest to deliver data to the server. Frontend: We will use a simple form containing name and email as an input and submit b
2 min read
How to Receive JSON Data at the Client Side ?
For sending and receiving data to or from the server JSON format is used. JSON stands for Javascript Object Notation and it is a widely used format nowadays because of its advantages and simplicity. In this article, we will use XML HttpRequest to receive data from the server and use NodeJS for the backend for sending JSON data. Example: The below e
2 min read
jQuery UI Sortable receive Event
jQuery UI is a web-based technology and consists of various GUI widgets, visual effects, and themes. These features can be implemented using the jQuery JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add widgets easily. The
3 min read
How to receive server-sent event notifications ?
This is a one-way communication that is made with the server by making a single persistent connection. Initially, the client makes a connection with the server and waits for the trigger of the event by the server and the server triggers the events on the client by sending some messages to the client. Now, we will learn to get notified from the serv
3 min read
How to Receive and Analyze Response in Postman ?
If you're involved in API development or testing, you know how crucial it is to receive and analyze responses accurately. Postman, a popular API development tool, provides robust features for handling responses efficiently. In this article, we'll explore how to receive and analyze responses effectively within Postman. Postman simplifies the process
7 min read
How To Fix Git Error: Pre-Receive Hook Declined?
When working with Git repositories, you may encounter an error message that says "Pre-receive hook declined." This error occurs when a pre-receive hook, a type of server-side Git hook, rejects your push. Pre-receive hooks are scripts that run on the server before the push is accepted, and they are often used to enforce rules about commits, branches
3 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more. Approach: We are going to fetch JSON data from one of free website,
2 min read
How to Post JSON Data using Curl ?
One can send the post data using curl (Client for URLs), a command line tool, and a library for transferring data with URLs. It supports various types of protocols. Most of the use cases of the curl command are posting JSON data to a server endpoint. CURLcURL stands for ( Client for URLs) and is a command line tool and library for transferring data
3 min read
How to Post JSON Data from JavaScript Frontend to FastAPI Backend?
In modern web development, the integration of frontend and backend technologies is crucial for building dynamic and interactive web applications. When it comes to communicating data between these two components, JavaScript and FastAPI are powerful tools. This article will guide you through the process of posting JSON data from a JavaScript frontend
3 min read
Article Tags :
three90RightbarBannerImg