简体   繁体   English

使用 curl 或 webhook 从外部服务器将数据发布到数据库

[英]Post data to database from external server using curl or webhooks

Here is the summary of what i want to archive and how far i have gone using curl which seems to do my task perfectly well by triggering a file in another sever to run and update the database, but my problem now is that the database updated is not with the data or values sent from the serverA where the html form is.这是我要存档的内容的摘要以及我使用 curl 的进展情况,这似乎通过触发另一台服务器中的文件来运行和更新数据库很好地完成了我的任务,但我现在的问题是更新的数据库是不包含从 html 形式所在的 serverA 发送的数据或值。

<!--- html code -->
<form action="" method="post" name="cashaform" id="cashaform">
<div class="dephone">Phone <br><input type="text" name="phone" id="phone" /></div>
<div class="deemail">Email <br><input type="text" name="email" id="email" /></div>
<div class="submit"> <br><input type="submit" name="submitprime" value="Submit" id="submitprime" /></div>

</form>

// the code that processes the form and sends it to do.php on another server that updates the database with details from the form // 处理表单并将其发送到 do.php 在另一台服务器上的代码,该服务器使用表单中的详细信息更新数据库

// This is process.php in serverA
if(isset($_POST['submitprime'])){
    
    $sphone = $_POST['phone'];
    $semail = $_POST['email'];

    $form_data = array(
    'phone' => $sphone,
    'email' => $semail
    );

    $str = http_build_query($form_data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/serverb/do.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
}

Now on serverB i have this file called do.php which updates the database once the process.php hits it through curl现在在服务器 B 上,我有一个名为 do.php 的文件,一旦进程更新数据库。php 通过 curl 命中它

// do.php that is on serverb

$hostname = 'localhost';
$dbusername = 'prime';
$dbpassword = 'prime';
$dbname = 'prime';
 
$conn = mysqli_connect($hostname, $dbusername, $dbpassword, $dbname);

$email = 'pandglo@yahoo.com';  // this values should be replaced with phone sent from form in serverA via curl
$phone = '09077345';           // this values should be replaced with email sent from form in serverA via curl


// update the database
$run = "INSERT INTO prime_securetable (email, phone) VALUES('$email', '$phone')";

if(mysqli_query($conn, $run) ) {
    echo 'Transferred Successfully';
}else{
    echo'transfer failed';
}

mysqli_close($conn);

I want the data phone and email that will be used to update the database be the one sent from the post from form in serverb, i tried it and set fixed values using pandglo@yahoo.com and a fixed phone 09077345 but i want this values to be what was actually submitted from the form in serverA and it updates the database, but i want it to be values from the form posted through curl.我想要将用于更新数据库的数据电话和 email 是从 serverb 中的表单发送的帖子,我尝试了它并使用 pandglo@yahoo.com 和固定电话 09077345 设置固定值,但我想要这个值成为从 serverA 中的表单实际提交的内容并更新数据库,但我希望它是通过 curl 发布的表单中的值。 Any help please.请提供任何帮助。

Very simply put, you can use this to get values posted to your serverb:简单地说,您可以使用它来获取发布到服务器的值:

$email = $_POST['email'];
$phone = $_POST['phone'];

Note your code is not production-ready as it is vulnerable for SQL injections, it also doesn't validates incoming data etc. Be sure to address this before using it on your live website.请注意,您的代码不是生产就绪的,因为它容易受到 SQL 注入的攻击,它也不会验证传入的数据等。请务必在您的实时网站上使用它之前解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM