Retrieving Request Payload in PHP
When utilizing PHP with frameworks like ExtJS and ajax stores, it is possible to encounter scenarios where request data is sent via the Request Payload instead of POST or GET. This data appears as JSON in the Chrome Console's "Request Payload" field, while $_POST and $_GET remain empty.
To retrieve this payload in PHP, utilize the following snippet:
$request_body = file_get_contents('php://input');
If the payload is in JSON format, you can parse it as follows:
$data = json_decode($request_body);
The variable $data will now contain the JSON data as a PHP array.
It's important to note that "php://input" is a read-only stream that facilitates direct access to the raw data from the request body. It is preferred over $HTTP_RAW_POST_DATA for POST requests and is a more memory-efficient alternative to activating "always_populate_raw_post_data" for scenarios where $HTTP_RAW_POST_DATA is not populated by default. However, "php://input" is not supported with "enctype="multipart/form-data"."
The above is the detailed content of How to Retrieve Request Payload in PHP with ExtJS and Ajax Stores?. For more information, please follow other related articles on the PHP Chinese website!