Reputation: 5185
I use php function file_get_contents
and one of params is http headers. I make them such like this:
if (strpos($key, 'HTTP_') === 0) {
$key = strtolower(strtr(substr($key, 5), '_', '-'));
$this->headers .= $key . ': ' . $value . '\r\n';
}
but here is a problem, I should send headers in double quotes like this:
"Connection: close\r\nContent-Length: $data_len\r\n"
Here is an example of how do I make request:
$opts = array(
'http' => array(
'method' => "GET",
'header' => $this->headers
)
);
$this->data = file_get_contents('http://phd.yandex.net/detect', false, stream_context_create($opts));
but it is fails. If I replace $this->headers
in array with a custom string of http headers, everything works fine.
how to make it works right?
Upvotes: 2
Views: 942
Reputation: 54719
The \r\n
needs to be in double quotes so that the characters are parsed correctly. Everything else can be appended using single quotes, no problem. Only a few things are parsed using the backslash in single quoted strings, such as \\
, \'
, and \"
.
Your headers are looking like this:
Key: Value\r\nKey: value\r\n
Where the \r\n
is appearing as an actual string, when you want it to look like this:
Key: Value
Key: Value
Where the \r\n
actually creates a new line in the headers.
Upvotes: 5