What are the best practices for handling line endings (CRLF vs LF) in HTTP requests in PHP?

When sending HTTP requests in PHP, it's important to ensure that the line endings are consistent with the HTTP specification, which typically uses CRLF (Carriage Return Line Feed) as the line ending. To ensure proper handling of line endings, you can use the PHP_EOL constant which represents the correct line ending for the current platform.

// Set the correct line ending for HTTP requests
$request = "GET / HTTP/1.1" . PHP_EOL;
$request .= "Host: example.com" . PHP_EOL;
$request .= "Connection: close" . PHP_EOL . PHP_EOL;

// Send the HTTP request
$response = file_get_contents("http://example.com", false, stream_context_create([
    'http' => [
        'method' => 'GET',
        'header' => $request
    ]
]));

echo $response;