Home  >  Article  >  Backend Development  >  How to Access Request Payload in PHP When $_POST and $_GET Are Empty?

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-10 16:00:04349browse

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');

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

$data = json_decode($request_body);

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!

Statement:
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