Let us first look at the below three 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 the 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 reads it into a string.
Later, we can use the json_decode() function to decode the JSON string.
Handling JSON POST request:
// Takes raw data from the request $json = file_get_contents('php://input'); // Converts it into a PHP object $data = json_decode($json);
- JSON | modify an array value of a JSON object
- HTTP GET and POST Methods in PHP
- How to select and upload multiple files with HTML and PHP, using HTTP POST?
- How to get form data using POST method in PHP ?
- How to post data using file_get_contents in PHP ?
- How to create an array for JSON using PHP?
- How to append data in JSON file through HTML form using PHP ?
- jQuery | post() Method
- Two most misunderstood terms GET and POST in web development
- How to display bootstrap carousel with three post in each slide?
- Perl | GET vs POST in CGI
- Get and Post method using Fetch API
- Simple POST request using the fetch API
- What is POST(Power-On-Self-Test)?
- How to post a file from a form with Axios?
- JSON Formatting in Python
- How to parse JSON in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.