I've come across a dumbfounding issue with sending headers in PHP. I've spent about 45 minutes reading on SO and other sites and I can't come up with a legitimate reason for my problem.
I need to send a POST request to another server and I'm using the PHP header() function to set the values. I have sample code below.
$server = 'http://fakedomain.com';
$server_path = '/';
$request = 'key=value&key2=value2';
header("POST $server_path HTTP/1.1" );
header("Host: $server\r\n" );
header("Content-type: application/x-www-form-urlencoded\r\n" );
header("Content-length: ".strlen($request)."\r\n" );
header("Connection: close\r\n\r\n" );
header($request);
I've tried a variety of options but each of them results in the same error in my log file
malformed header from script. Bad header=POST / HTTP/1.1: php5.cgi
I'm an experience PHP programmer who just hasn't had much need to manually send HTTP post requests.
I want the code to redirect the browser so that's why I decided to use this method.
Am I doing it right?
Is there some other way that is standard and I just don't know about?