将php变量从一台服务器传输到另一台服务器


transfer php variables from one server to another

我有一个联系人表单,它将输入发布到位于我的服务器中的.php文件中
一些代码:

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

在我的服务器中,我不能使用php-mail(),所以我想将这些变量转移到其他域中的另一个.php文件中。

我知道我可以直接用的形式

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

但我希望php脚本在我的服务器上,并且"幕后"传输变量。我的第一个问题是,是否可以这样做?第二,如何…

您需要使用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);

您可以使用file_get_contents()发送post请求(例如):

// 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)
    *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','$Cessage')";$res=mysqli_query($link,$sql);