Send post variables from a script
The following shows how to create a form that submits to a middle script that takes some post variables and merges them into one variable and then posts that to another script that can parse the POST.
index.php posts to middle.php
<?php $html = <<<eof <html> <head> <title>form</title> </head> <body> <form action="middle.php" method="post"> <input type="hidden" name="field1" value="this is field 1" /> <input type="hidden" name="field2" value="this is field 2" /> <input type="hidden" name="field3" value="this is field 3" /> <input type="submit" value="submit" /> </form> </body> </html> eof; print($html); ?>
midde.php uses Open Internet or Unix domain socket connection to post to last.php
<?php
$description = "description=" . $_POST['field1'] . "<br />" . $_POST['field2'] . "<br />" . $_POST['field3'];
$content_length = mb_strlen($description);
$content = $description;
$url = "http://www.example.com/post/last.php";
$host = "www.example.com";
### Keep the blank line after Content-Length and keep the \n at the very last line.
$headers = <<<eof
POST {$url} HTTP/1.1
Host: {$host}
Content-Type: application/x-www-form-urlencoded
Content-Length: {$content_length}
{$content}\n
eof;
$socket = fsockopen($host, 80);
$idx = 0;
fputs($socket, $headers);
while(!feof($socket))
{
$result[$idx++] = fgets($socket, 128);
}
?>
last.php
This last script just writes the output of print_r($_POST) to a file named out.txt. This just demonstrates the post was actually sent correctly.
<?php
ob_start();
print_r($_POST);
$output = ob_get_contents();
file_put_contents("out.txt", date("Y-m-d H:i:s") . "\n\n{$output}");
ob_end_clean();
?>
http://www.alt-php-faq.org/local/55/ »
[Click to add or edit comments])
Please prepend comments below including a date