phasync
FormBody / for

Method

FormBody::for

The form of a request with this method and Content-Type, or null when PHP would not parse its body.

Description#

public static FormBody::for(
    string $method,
    string $contentType,
    StreamInterface $body
): ?FormBody

Parameters#

$method string
$contentType string
$body StreamInterface

Return values#

?FormBody

Examples#

Example 1Which requests have a form
use phasync\Psr\StringStream;
use Swerve\Psr\FormBody;

// PHP parses a POST with a form's Content-Type; every other body stays raw
$requests = [
    ['POST', 'application/x-www-form-urlencoded'],
    ['POST', 'multipart/form-data; boundary=x'],
    ['POST', 'application/json'],
    ['PUT', 'application/x-www-form-urlencoded'],
];
foreach ($requests as [$method, $type]) {
    $form = FormBody::for($method, $type, new StringStream(''));
    echo str_pad("$method $type", 45), null === $form ? 'raw' : 'form', "\n";
}
Output
POST application/x-www-form-urlencoded       form
POST multipart/form-data; boundary=x         form
POST application/json                        raw
PUT application/x-www-form-urlencoded        raw