Web Hosting Talk







View Full Version : How to Post from a PHP script?


okok
01-22-2005, 04:08 AM
I need to write a PHP script that posts information to a remote server the way a browser would post information filled in in an HTML form, receive information from the remove server using the same method, and finally display the result to the user.

Any idea how to do that?

Vult-r
01-22-2005, 07:43 AM
this could be in your HTML:

<form action="somescript.php" method="post">
<input type="hidden" name="postedvalue" value="bla">
<input type="submit" name="submit" value="submit">
</form>

Then create a small php script (somescript.php)

you can retrieve the value with:
$postedvalue = $_POST['postedvalue];

then show result with 'header':
header('Location: result.php');

okok
01-22-2005, 09:22 AM
Thank you, but this is not what I meant. I probably didn't explain my intention properly.

I want the PHP script to Post some data to remote server, get something back from the remote server, process the reply and only then echo something to the end user. Or in other words, I want the PHP script to post information without the submission of any HTML form.

Xenatino
01-22-2005, 04:58 PM
DO you want to submit data to a MySQL database on a remote server, or is there a form on a remote server that needs to be filled in without the user filling it in if that makes sense?

Angelo
01-22-2005, 06:04 PM
This is it:
check for synthax errors


$post = "variable1=$my_variable";
$post .= "&variable2=".urlencode(stripslashes(unhtmlentities($my_variable2)));

$fp = fsockopen ('www.domain.com', 80, $errno, $errstr, 30);
$header .= "POST /wheretopost.cgi HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($post) . "\r\n\r\n";

if (!$fp) {
$error .= "$errstr ($errno)";
}
else {
fputs ($fp, $header.$post);
fclose($fp);
}