gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:57:59 25 4
gpt4 key购买 nike

我有一个联系表单,它将输入发布到位于我的服务器中的 .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);

关于php - 将 php 变量从一台服务器传输到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17618484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com