What best practices should be followed when building the content array for stream context in PHP?

When building the content array for stream context in PHP, it is important to follow best practices to ensure proper data handling and security. This includes properly encoding data, setting appropriate headers, and validating input to prevent injection attacks.

// Example of building the content array for stream context in PHP
$content = http_build_query(array(
    'username' => 'john_doe',
    'password' => 'securepassword123'
));

$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => "Content-type: application/x-www-form-urlencoded\r\n"
                    . "Content-Length: " . strlen($content) . "\r\n",
        'content' => $content
    )
));

// Use the stream context in your HTTP request
$response = file_get_contents('http://example.com/api', false, $context);