Oihana PHP System

getBodyParam.php

Table of Contents

Functions

getBodyParam()  : mixed
Retrieves a single parameter from the HTTP request body.

Functions

getBodyParam()

Retrieves a single parameter from the HTTP request body.

getBodyParam(ServerRequestInterface|null $request, string $name) : mixed

This helper extracts a value from the parsed request body ($request->getParsedBody()), supporting dot notation for nested structures (e.g. 'user.address.city').

The body is internally normalized into a fully associative array using toAssociativeArray(), ensuring compatibility with both array and stdClass-based JSON payloads.

It internally uses hasKeyValue() and getKeyValue() from the oihana\core\accessors namespace.

If the request is null, or if the specified parameter does not exist, the function returns null.

Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance.

$name : string

The parameter name or nested key path (e.g. 'geo.latitude').

Tags
example

Retrieve a flat parameter:

// POST body: ['name' => 'Alice']
echo getBodyParam($request, 'name'); // 'Alice'

Retrieve a nested parameter using dot notation:

// POST body: ['geo' => ['latitude' => 42.5, 'longitude' => 1.5]]
echo getBodyParam($request, 'geo.latitude'); // '42.5'

Handle missing keys or null request:

echo getBodyParam(null, 'foo');            // null
echo getBodyParam($request, 'not.exists'); // null
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
mixed

The parameter value if found, or null otherwise.


        
On this page

Search results