我有一个联系表单,将输入发布到我服务器上的.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脚本在我的服务器上并且“幕后”转移变量。 我的第一个问题是,是否有可能以这种方式这样做?第二,如何...
答案 0 :(得分: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);
答案 1 :(得分: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)
答案 2 :(得分: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);