View Full Version : post hidden variables to another site without submitting?

05-03-2006, 09:14 AM
I have a bit of a weird problem. I have a form on a page that posts to itself. There are 3 radio button options on the form. With 2 of the options the form is handled by itself, updates the database, sends some emails and finishes. But if the 3rd option is selected I need to "post" a bunch of hidden variables to another website completely. Is there some way to post a form from inside my "submit" code? Or maybe I can change the place the form is posted to when the radio button is selected and have the hidden variables set? Any ideas? Thanks in advance.

05-03-2006, 09:25 AM
you can use curl, it lets you put together the bits you need to send, and then it does the post request for you.


05-03-2006, 03:39 PM
You could also use fsockopen

$httpRequest = Array();
$errors = Array();
// Create the HTTP Request
$postContent =
$httpRequest[] = "POST /subfolder/ HTTP/1.0";
$httpRequest[] = "Host: secure.domain.com";
$httpRequest[] = "Content-Type: application/x-www-form-urlencoded";
$httpRequest[] = "Content-Length: ". strlen($postContent) ;
$httpRequest[] = "Connection: Close";
$httpRequest[] = "";
$httpRequest[] = $postContent;
// Open socket connection
// Send HTTP Request
// Read HTTP Response
$httpResponse = "";
$secureSocket = fsockopen("ssl://secure.domain.com",443,$errno, $errstr, 3);
if ( !$secureSocket || ! is_resource($secureSocket) ) {
$errors[] = "Could not establish connection [$errno : $errstr]";
} elseif( fwrite( $secureSocket , join("\n",$httpRequest) ) ) {
while (!feof($secureSocket)){
$httpResponse .= fgets($secureSocket,128);
} else {
$errors[] = "Could not write to secure connection";

05-03-2006, 10:43 PM
I have been trying the fsockopen suggestion. I think I must be missing something. I got this example from the link degsy posted.

If you want to post $_POST vars and (in this case) one file named userfile to $remote_server and $remote_url.

// get the necessary data
$file_name = $_FILES['userfile']['name']; // the file
$tmp_name = $_FILES['userfile']['tmp_name']; // the file
$content_type = $_FILES['userfile']['type']; // the file mime type

$boundary = "---------------------".substr(md5(rand(0,32000)),0,10);

// Build the header
$header = "POST $remote_url HTTP/1.0\r\n";
$header .= "Host: $remote_server\r\n";
$header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";
// attach post vars
foreach($_POST AS $index => $value){
$data .="--$boundary\r\n";
$data .= "Content-Disposition: form-data; name=\"".$index."\"\r\n";
$data .= "\r\n".$value."\r\n";
$data .="--$boundary\r\n";
// and attach the file
$data .= "--$boundary\r\n";
$content_file = join("", file($tmp_name));
$data .="Content-Disposition: form-data; name=\"userfile\"; filename=\"$file_name\"\r\n";
$data .= "Content-Type: $content_type\r\n\r\n";
$data .= "".$content_file."\r\n";
$data .="--$boundary--\r\n";
$header .= "Content-length: " . strlen($data) . "\r\n\r\n";
// Open the connection
$fp = fsockopen($remote_server, 80);
// then just
fputs($fp, $header.$data);
So I have set the $remote_server, $remote_url and added some variables to post to $data. When the code executes I think it is posting the variables to the test.php page I am using, but I need it to open that page so it can show me the variables. The code runs without errors, but doesn't open the page I have posted to. What have I missed?

05-04-2006, 06:34 AM
Tis cool, I got curl working for me :D

05-04-2006, 07:24 AM
Well, good for you. I was going to offer you a fix for your fsockopen code

05-04-2006, 03:27 PM
For future reference. This kind of coding doesn't open the page. It sends a request and recieves a response.

05-04-2006, 11:25 PM
For future reference. This kind of coding doesn't open the page. It sends a request and recieves a response.

Thanks for that. I should have explained myself better. I was trying to send shopping cart variables to an external payment form, so I need to send the variables and open the page. But I only need to do this if "credit card" was the selected payment method, hence the problem. Determine which method of payment and then go to the remote server if its for credit card.

With curl I used this line:-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); which opens the page. I have tried it on a test page I made, and it seems to work OK. Thanks again.

08-15-2006, 01:00 AM
...unfortunately my client's server doesn't have curl, so I can't copy Taipan's solution.

I have a similar function that POSTs data, which I use to send users to a credit card authorisation site (I want to do this without them pressing a 'submit' button on a form, because several things have to happen at that moment: an order is created in a local orders table, and the user is sent off to get authorisation from an external source).

My problem is that, while my function sends the POST variables ok, and returns the resulting page into an array, it doesn't actually forward the browser to the remote site with those variables so that the can be processed together in the remote script.

You'll see from the commented out line, that I tried a header("Location: [target url]"); - but that sends a seperate redirect, without the POSTed variables, so the user arrives at a script that responds 'error, no data sent'.

Grr! Any solutions anyone? I've been surfing for hours, and I'm sure I'm really close! I suspect that I should be able to process the returned '$result' data somehow - it returns the whole resulting page that I was expecting, complete with what looks like headers a the top....

Here's my function:

function post_data($datastream, $url)

if (substr($originalurl, 0, 5) == "https") {
$protocol = "https";
$url = preg_replace("@^https://@i", "", $url);
$port = 443;
} else {
$protocol = "http";
$url = preg_replace("@^http://@i", "", $url);
$port = 80;

$host = substr($url, 0, strpos($url, "/"));
$uri = strstr($url, "/");

$reqbody = "";

foreach($datastream as $key=>$val) {
if(is_array($val)){ //don't url encode if we're passing an array

if (!empty($reqbody)) $reqbody .= "&";
$reqbody .= $key."=".$val;


if (!empty($reqbody)) $reqbody .= "&";
$reqbody .= $key."=".urlencode($val);


$reqlength = strlen($reqbody);

$reqheader = "POST $uri HTTP/1.0\r\n".
"Host: $host\r\n" . "User-Agent: CK-Conception POST-o-matic\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: $reqlength\r\n\r\n".

# header("Location: $originalurl");

if (substr($originalurl, 0, 5) == "https") {
$socket = fsockopen("ssl://" . $host, $port, $errno, $errstr);
} else {
$socket = fsockopen($host, $port, $errno, $errstr);

if (!$socket) {
$result["errno"] = $errno;
$result["errstr"] = $errstr;
return $result;
fputs($socket, $reqheader);
while (!feof($socket)) {
$result[] = fgets($socket, 4096);

return $result;

Any assistance would be greatly appreciated