2

サーバーにある.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 つ目は、どのように...

4

3 に答える 3

3

あなたは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);
于 2013-07-12T15:35:28.660 に答える
2

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)
于 2013-07-12T15:32:37.853 に答える