将php变量从一个服务器转移到另一个服

将php变量从一个服务器转移到另一个服

问题描述:

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...

我有一个联系表单,将输入发布到我服务器中的.php文件。
some代码: p>

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

在我的服务器中我无法使用php邮件 (),所以我想将这些变量传递给位于其他域的另一个.php文件。 p>

我知道我可以直接以 p>

  action =“http://otherdomain.com/contact.php”的形式完成 
  code>  pre> 
 
 

但我希望php脚本在我的服务器上并且“幕后”传输变量​​。 我的第一个问题是,是否有可能这样做 这条路? 第二,如何...... p> div>

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);

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
",
    'data' => $body
  )
);

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

// do request    
$response = file_get_contents('http://other.server/', false, $context)