ZetCode

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.

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.

JSON Request Parsing

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.

File Upload Handling

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.

Raw Input Handling

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.

Content-Type Detection

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.

Best Practices

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

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 HTTP Functions.