PHP request_parse_body Function
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.
Basic Definition
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.
Basic Form Data Parsing
This example demonstrates parsing standard form-urlencoded POST data.
<?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.
JSON Request Parsing
This shows how to parse JSON data sent in the request body.
<?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.
File Upload Handling
This example demonstrates handling file uploads with request_parse_body.
<?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.
Raw Input Handling
This shows how to access the raw request body when needed.
<?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.
Content-Type Detection
This example demonstrates how the function handles different content types.
<?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.
Best Practices
- 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
Source
PHP request_parse_body Documentation
This tutorial covered the PHP request_parse_body
function with practical
examples for handling different HTTP request body types.
Author
List all PHP HTTP Functions.