PHP request_parse_body function tutorial shows how to parse HTTP request bodies in PHP. Learn request_parse_body with practical examples for handling form data, JSON, and file uploads.
last modified April 4, 2025
The PHP request_parse_body function parses HTTP request bodies. It handles various content types like form data, JSON, and file uploads.
request_parse_body processes the raw HTTP request body content. It automatically detects the content type and parses accordingly.
Syntax: request_parse_body(): array. Returns parsed data as an array. Works with POST, PUT, and PATCH requests. Requires PHP 8.1 or later.
This example demonstrates parsing standard form-urlencoded POST data.
form_parsing.php
<?php
declare(strict_types=1);
$data = request_parse_body(); echo “Username: " . ($data[‘username’] ?? ‘’); echo “Password: " . ($data[‘password’] ?? ‘’);
This code parses form data sent with application/x-www-form-urlencoded. The function returns an associative array of form fields and values.
This shows how to parse JSON data sent in the request body.
json_parsing.php
<?php
declare(strict_types=1);
$data = request_parse_body(); echo “Name: " . ($data[’name’] ?? ‘’); echo “Age: " . ($data[‘age’] ?? ‘’);
When the Content-Type is application/json, the function automatically decodes the JSON into a PHP array. No manual json_decode needed.
This example demonstrates handling file uploads with request_parse_body.
file_upload.php
<?php
declare(strict_types=1);
$data = request_parse_body(); $file = $data[‘file’] ?? null;
if ($file && $file[’error’] === UPLOAD_ERR_OK) { move_uploaded_file($file[’tmp_name’], ‘uploads/’ . $file[’name’]); echo “File uploaded successfully”; }
For multipart/form-data requests, file uploads are automatically processed. The file data structure matches $_FILES but is merged with other form data.
This shows how to access the raw request body when needed.
raw_input.php
<?php
declare(strict_types=1);
$data = request_parse_body(); $raw = file_get_contents(‘php://input’);
echo “Parsed data: “; print_r($data); echo “Raw data: " . $raw;
While request_parse_body handles parsing, you can still access raw input. This is useful for custom content types or debugging purposes.
This example demonstrates how the function handles different content types.
content_type_detection.php
<?php
declare(strict_types=1);
$data = request_parse_body(); $contentType = $_SERVER[‘CONTENT_TYPE’] ?? ‘’;
echo “Content-Type: $contentType\n”; echo “Parsed data: “; print_r($data);
The function automatically detects and processes different content types. It supports form data, JSON, and multipart form data out of the box.
Validation: Always validate parsed data before use
Error Handling: Check for parsing errors
Memory: Be mindful of large file uploads
Security: Sanitize user input
Content-Type: Verify expected content types
PHP request_parse_body Documentation
This tutorial covered the PHP request_parse_body function with practical examples for handling different HTTP request body types.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.