2

I have a contact form which post the input to a .php file located in my server.
some of the code:

$name_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$message_field = $_POST['message'];

In my server I can't use php mail(), so I want to transfer this variables to another .php file located in other domain.

I know I can do it directly in the form

action="http://otherdomain.com/contact.php"

but I want the php script to be on my server and "Behind the scenes" transfer the variables. My first question is if it possible to do so in this way? and the second, how to...

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Ben
  • 885
  • 8
  • 16

3 Answers3

3

You will want to use CURL

$url = 'http://www.otherdomain.com/contact.php';
$fields_string = http_build_query($_POST);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
Chris Morrissey
  • 489
  • 2
  • 4
2

You can send the post request using file_get_contents() (for example) :

// example data
$data = array(
   'foo'=>'bar',
   'baz'=>'boom',
);

// build post body
$body = http_build_query($data); // foo=bar&baz=boom

// options, headers and body for the request
$opts = array(
  'http'=>array(
    'method'=>"POST",
    'header'=>"Accept-language: en\r\n",
    'data' => $body
  )
);

// create request context
$context = stream_context_create($opts);

// do request    
$response = file_get_contents('http://other.server/', false, $context)
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
0
    *Here is the code to send data from one domain to another using curl in php*     `$url='http://training3.mbincbs.com/karma/phpcontact.php';
$s = curl_init();
curl_setopt($s,CURLOPT_URL, $url); 
curl_setopt($s, CURLOPT_POST, 1);
curl_setopt($s, CURLOPT_POSTFIELDS,

        "contactmessage=".$message."&contactname=".$name."&contactemail=".$email."&contactsubject=".$subject);

curl_setopt($s, CURLOPT_RETURNTRANSFER, true);

echo $server_output = curl_exec($s); curl_close($s);

phpcontact.php

`$Name = $_POST["contactname"];
`$Email = $_POST["contactemail"];
`$Subject = $_POST["contactsubject"];
`$Message = $_POST["contactmessage"];

if(isset($link)){

$sql="INSERT INTO Contact (contactname,contactemail,contactsubject, contactmessage) VALUES ('$Name','$Email','$Subject', '$Message')"; $res=mysqli_query($link,$sql);

Ajmal
  • 99
  • 1
  • 6