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?
The header functions relate to the headers returned to the client, I suggest you look into using cURL to do your post request: http://www.php.net/cURL
header() sends a response header.
It sounds like you want to make a request, on the back end.
So you probably want to use curl to make the request.
If when you're done processing the response, you want to send some kind of header to the user agent (browser), then header() would be appropriate.
Expanding on timdev's answer, here's the cURL you'd use:
I have tried simple html form posting it worked for me
If you want to redirect user to another page, you should use
If you want the user to send POST variables to the redirect page you will need to use JavaScript.