Home > Backend Development > PHP Tutorial > How Do I Read a JSON POST Request Body in PHP?

How Do I Read a JSON POST Request Body in PHP?

Patricia Arquette
Release: 2024-11-30 09:08:09
Original
668 people have browsed it

How Do I Read a JSON POST Request Body in PHP?

Reading HTTP Request Body from a JSON POST in PHP

You're facing an issue reading a JSON object POSTed to your PHP script. Despite successfully registering the endpoint and receiving the request, you're unable to access the JSON body.

Solution:

In order to parse the JSON POSTed body, you simply need:

  1. Use file_get_contents('php://input') to read the raw JSON body.
  2. Call json_decode($inputJSON, TRUE) to convert the JSON into an array, where the TRUE flag ensures the resulting value is an array.

For example:

$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE); //convert JSON into array
Copy after login

This will provide you with an array containing the POSTed JSON object, allowing you to access and manipulate its data as needed.

The above is the detailed content of How Do I Read a JSON POST Request Body in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template