2013-07-12 199 views
2

我有一个联系表单,它将输入发布到位于我的服务器中的.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脚本是我的服务器和“幕后推手”传递变量上。 我的第一个问题是,如果有可能这样做?第二,如何...

+0

你总是可以使在自己的服务器上运行一个php页面,然后从那里传递到另一个域。 – codersarepeople

+0

是的,这是我想要的。这个怎么做? – Ben

+0

'fsockopen();'<3'curl _ *();'<3 – DanFromGermany

回答

3

您将要使用卷曲

$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); 
+0

使用'CURLOPT_FORBID_REUSE'有什么好处? – hek2mgl

+0

适合我!谢谢! – Ben

2

您可以发送使用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) 
+0

谢谢,但由于某种原因,表单输入在远程站点上为空 – Ben

+0

? – hek2mgl

+0

我如何检查问题在哪里?收到的电子邮件为空 – Ben

相关问题