Переносить переменные php с одного сервера на другой

У меня есть контактная форма, которая публикует ввод в файл.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 был на моем сервере, а "за кулисами" передавал переменные. Мой первый вопрос: возможно ли это сделать таким образом? и второе, как...

2 ответа

Решение

Вы хотите использовать 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() (например):

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

эхо $ 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 = "ВСТАВИТЬ В контакт (контактное имя, контактное сообщение, контактное сообщение, контактное сообщение) ЗНАЧЕНИЯ ('$ Имя','$ Электронная почта','$ Тема','$ Сообщение')"; $res=mysqli_query($ ссылка, $ sql);

Другие вопросы по тегам