PHP function \http_parse_headers()
parses HTTP headers into an associative array.
But is there some reverse function? Which parses associative array into HTTP headers?
Cannot find anything :(
(it's for saveing email into textual .eml
file)
PHP function \http_parse_headers()
parses HTTP headers into an associative array.
But is there some reverse function? Which parses associative array into HTTP headers?
Cannot find anything :(
(it's for saveing email into textual .eml
file)
There isn't a function that turns associative array into text-representation of headers. The reason: this function is extremely trivial to create.
Headers are defined as key: value
delimiter is
.
There is another
delimiter between headers and body.
Lets take an example array:
$headers = [
'Content-Length': 50,
'Content-Encoding': 'gzip'
];
The goal: provide a string that represents HTTP headers
function parse_array_to_headers(array $headers)
{
$result = [];
$delimiter = "
";
foreach($headers as $name => $value)
{
$result[] = sprintf("%s: %s", $name, $value);
}
return implode($delimiter, $result);
}
Note: this function will not check the validity of array and it won't return the string with two repetitions of
at the end. This example serves to show how easy it should be to add missing function. Adjust according to your needs. Also, I didn't test this so don't copy paste it! :)