For the benefit of others, I want to show how to do
an HTTP POST request using fsockopen(). I banged my
head against a wall for two days trying to figure
this out. I even went to http://php.net/ to find
out how to do this, but it didn't help because
my mind automagically converted the "Content-Length"
header into "Length."
I even went to http://www.ietf.org/ to find out
the specific protocol for HTTP POSTs, but I didn't
find it there. RFC 2616 (HTTP) does not give the
exact format for POST requests.
Question: Where is the format for POST requests defined?
---------------- send_post.php ---------------
<?php
// file: send_post.php
error_reporting(E_ALL & ~E_NOTICE);
$eol = "\r\n";
$errno = 0;
$errstr = '';
$data = 'username=george&password=i81bpz';
$fid = fsockopen('localhost', 8080, &$errno, &$errstr, 30);
if ($fid) {
fputs ($fid, "POST /post/show_post.php HTTP/1.1$eol");
fputs ($fid, "HOST: localhost$eol");
fputs ($fid, "Connection: close$eol");
fputs ($fid, "Content-Type: application/x-www-form-urlencoded$eol");
// Use 'Content-Length' NOT 'Length' !
fputs ($fid, 'Content-Length: ' . strlen($data) . $eol);
fputs ($fid, $eol);
fputs ($fid, $data);
fputs ($fid, $eol);
fpassthru($fid);
}
?>
------------- show_post.php -----------------
<?php
// file: show_post.php
error_reporting(E_ALL & ~E_NOTICE);
echo "<pre>\n";
echo "------------ POST VARIABLES ----------\n";
print_r($HTTP_POST_VARS);
echo "------------ SERVER VARIABLES ----------\n";
print_r($HTTP_SERVER_VARS);
echo "</pre>\n";
?>