Rhaoma Posted March 11 Share Posted March 11 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 Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/ Share on other sites More sharing options...
Rhaoma Posted March 12 Author Share Posted March 12 (edited) If anyone stumbles into the same problem I suggest moving server requests to php cURL. Making the request through curl fixes things, as curl automatically sets the content-length header correctly - for some reason... Unsatisfying, that I couldn't figure out why file_get_contents failed, but at least there is a fix to some extent Edited March 12 by Rhaoma Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/#findComment-1651271 Share on other sites More sharing options...
Solution jodunno Posted March 13 Solution Share Posted March 13 On 3/11/2025 at 10:17 PM, Rhaoma said: The below sample code hangs when adding the 'Content-Length: <some value>" header array item, otherwise works perfectly fine. where in the hell-p do you get 100 for the Content-Length? what is that supposed to mean? 100 what? sheep? usually, programmers let php determine the size of something. filesize, strlen() etc. I think that you should take a closer look at your code. Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/#findComment-1651297 Share on other sites More sharing options...
Rhaoma Posted March 13 Author Share Posted March 13 Well originally I of course calculated Content-Length based on my actual POST-/multipart-data size . I should of course have specified that the given excerpt was psudeo code. EDIT: I just rechecked, and retried the code i posted above, and it works 😕 - even if the content-length is 100 - the request gets posted. I'll have to agree with @jodunno that I simply wrote some shit code; where I do not know though Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/#findComment-1651306 Share on other sites More sharing options...
jodunno Posted March 13 Share Posted March 13 Hi Rhaoma, umm, I didn't imply or intend to imply that you write "sh1t code". I am not exactly a top notch coder. I actually write some ugly Spaghetti code at times. Noone would say that my coding skills are that to be admired, so i definitely do not insult other coders. Not sh1t code. I just see a 100 and i wonder what you are doing but i do not know how i solved any problems for you. If your code is working and you are happy with the code, then i suppose that you have solved your own problem. Otherwise, let us know if you are still having trouble. The forum has alot of pro php coders. Sometimes people get busy and may not respond immediately but someone will eventually come along and help. I hope that you have a pleasant day, John Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/#findComment-1651310 Share on other sites More sharing options...
Rhaoma Posted March 13 Author Share Posted March 13 Hi No offense taken at all ; I was just annoyed at myself with the fact that I spent most of last night debugging something, that seemingly fixed itself next morning (as is often sadly the case) Thats propably why my reply seemed angry. I apologize. As for the actual problem I have no idea what caused the initial behaviour. Now I get perfectly reasonable results regardless of whether i use cURL or file_get_contents; I therefore thought it was fitting to mark your reply as the solution, since: 16 hours ago, jodunno said: I think that you should take a closer look at your code. was exactly what was needed. Best, David 1 Quote Link to comment https://forums.phpfreaks.com/topic/326987-content-length-header-with-file_get_contents/#findComment-1651352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.