Hi
I'm acessing a backend server from php through file_get_contents. For most requests manually setting the Content-Length header has not been neccessary, but when trying to send multipart-form-encoded data, PHP seemingly didn't automatically add the Content-Length header.
I thought no biggie, I will just add it manually in the stream_context_create call along with my other headers. This however failed miserably; it seems that manually adding the Content-Length header, causes the HTTP request to not get sent (verified through wireshark). This is the case for all calls, whether multipart or not - the request simply doesn't get dispatched. PHP just freezes/hangs until the specified timeout, and then throws following warning: "WARNING: file_get_contents(http://127.0.0.1:83/items): Failed to open stream: HTTP request failed!". I have independently checked that, if the request was to be dispatched through other means than PHP the request succeeds.
The below sample code hangs when adding the 'Content-Length: <some value>" header array item, otherwise works perfectly fine.
$headers = array(
'Authorization: Bearer '.$User->GetAuthString(),
'Accept: application/json',
'Content-Length: 100'
);
$content = json_encode($_POST);
$url = getBackendURL().'/items';
// Forward request to backend
$context = stream_context_create(
array(
'http' => array(
'method' => $method,
'header' => $headers,
'content' => $content,
'timeout' => 300
)
)
);
$reply = file_get_contents($url, false, $context);
Any suggestion as to what is going on? I have tested everything i have come across on the web, header string instead of array, HTTP 1.0 instead of 1.1, "Connection: close" etc. I am at a loss
Regards