View Full Version : Form submission from PHP

03-16-2007, 12:39 PM

I need to perform a HTTP POST from a PHP script. Essentially my PHP script is receiving form data and making sure it is complete and valid before submitting it to another site.

I have used CURL and the streams layer to sucessfully make the POST from PHP, but they act as clients, whereas what I want to do is emulate a true browser form submission by issuing the correct HTTP headers. Here is what I have so far:


$post_data = 'var1=123&var2=456';
$content_length = strlen($post_data);

header('POST /test/test.php HTTP/1.1');
header('Host: localhost');
header('Connection: close');
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: ' . $content_length);



At the moment, this causes Apache to throw an internal error. I have tested the same code on IIS with the same result. I have also tested the script with PHP 4 & 5 - again same result.

I have even simplified the script to perform a GET:


header('GET /test/test.php HTTP/1.1');
header('Host: localhost');



This too causes a server error.

What in the blazes am I doing wrong?

03-16-2007, 06:07 PM
Not sure if this would fit your requirements, but you could redirect to a PHP page that immediately submits a form via <body onload()="submitform();"> and let the natural order of things take care of it.

03-16-2007, 06:26 PM
Just outputting headers in a php script is non-specific and not targeted at anything (actually they are sent to what ever made the http request to your web server to serve up your script file.)

Curl and stream/socket requests are targeted at a specific URL/port. If you want your curl/socket submission to have a complete set of headers, you must include those headers in the curl/socket output.

03-16-2007, 07:08 PM
Thanks for your reply CFMaBiSmAd

...actually they are sent to what ever made the http request to your web server to serve up your script file.

This is actually exactly what I want to do.

The idea is much the same in priniciple as when you process a form submission and issue a location header to send the browser to a different page:


header('Location: http://www.mysite.com/test.php');


The only difference is that I want is to issue the headers to make the browser perform a POST request instead, taking the browser to the destination page.

As I said - CURL and the streams layer act as clients - they make a connection from PHP and they also return the results within PHP which does not affect the browser at all.