How to Access Request Payload in PHP When $_POST and $_GET Are Empty?

Mary-Kate Olsen
Release: 2024-11-10 16:00:04
Original
312 people have browsed it

How to Access Request Payload in PHP When $_POST and $_GET Are Empty?

How to Access Request Payload in PHP

When using ExtJS and ajax store, request data may be sent in the HTTP request body instead of through POST or GET parameters. Consequently, $_POST and $_GET remain empty.

Solution

The request body can be accessed using the following PHP code:

$request_body = file_get_contents('php://input');
Copy after login

If the payload is in JSON format, it can be decoded with:

$data = json_decode($request_body);
Copy after login

This will populate the $data variable with an array representing the JSON payload.

Background on php://input

php://input is a read-only stream wrapper that provides access to raw data from the request body. It is particularly useful for POST requests and offers advantages over $HTTP_RAW_POST_DATA in terms of memory efficiency and independence from specific php.ini directives. Note that php://input is not available when using enctype="multipart/form-data".

The above is the detailed content of How to Access Request Payload in PHP When $_POST and $_GET Are Empty?. 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