Fraggy
Fraggy

Reputation: 579

Submit form on one server, process it and then post results to another domain

I have a form on one page with the action set to /process.php

Within the process.php I have the validation of the form and also it writes to a database on the server it sits on.

What I'd like to do after it has written to the database, is post the variables to another domain that will then process those variables differently.

Upvotes: 1

Views: 2721

Answers (2)

fortune
fortune

Reputation: 3382

you can use file_get_contents for the same.

Example:

$name="foobar";
$messge="blabla";

$postdata = http_build_query(
    array(
        'name' => $name,
        'message' => $message
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = @file_get_contents('http://yourdomain.com/process_request.php', false, $context);

 if($http_response_header[0]=="HTTP/1.1 404 Not Found"):

            echo "iam 404";

 elseif($http_response_header[0]=="HTTP/1.1 200 OK"):

            echo "iam 200";

 else:

            echo "unknown error"; 

 endif;

Upvotes: 0

user1744166
user1744166

Reputation:

Example using curl:

$name = 'Test';
$email = '[email protected]';

$ch = curl_init(); // initialize curl handle
$url = "http://domain2.com/process.php"; 
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "text=$name&name=$email"); // post fields
$data = curl_exec($ch); // run the whole process
curl_close($ch);

Example without curl: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

Upvotes: 1

Related Questions