Request Payload Retrieval in PHP with Ajax Store
In a scenario involving PHP, ExtJS, and an ajax store, data is transmitted not via POST or GET parameters, but rather in the "Request Payload" field as JSON. While the traditional $_POST and $_GET variables remain empty, this raises the question of how to retrieve this data effectively in PHP.
The solution lies in leveraging the php://input pseudo-file. This wrapper stream provides access to raw data from the request body. To obtain the request payload, simply utilize the file_get_contents() function:
$request_body = file_get_contents('php://input');
If the payload is in JSON format, you can further decode it using json_decode():
$data = json_decode($request_body);
The resulting $data variable will now contain the decoded JSON data as a PHP array, enabling easy access and manipulation.
Note that php://input is specifically suitable for processing raw data, and is a more efficient alternative to using $HTTP_RAW_POST_DATA. Additionally, it is not supported for requests with multipart/form-data content.
The above is the detailed content of How to Retrieve Request Payload Data in PHP with Ajax Store?. For more information, please follow other related articles on the PHP Chinese website!