简体   繁体   English

如何使用 HTTP:PostAsync() 将 JSON 数据提交到我的服务器? (罗布乐思)

[英]How do I use HTTP:PostAsync() to submit JSON data to my server? (ROBLOX)

So I understand how GetAsync works, and I have scripts on my server that work with what I'm trying to accomplish.所以我了解 GetAsync 是如何工作的,并且我的服务器上有一些脚本可以与我想要完成的工作一起工作。 But in terms of getting data to the server instead of reading it from a json, how would I do that?但就将数据获取到服务器而不是从 json 读取数据而言,我该怎么做呢? On the server side, my PHP Script that gets all the post data from the submitted JSON and checks a database with all the information and if its not there, it inserts it.在服务器端,我的 PHP 脚本从提交的 JSON 获取所有发布数据,并检查包含所有信息的数据库,如果不存在,则将其插入。 (It's a ban system. But I'm not sure if I have roblox-side set up correctly or if i have server-side set up correctly. Here's my code: (这是一个禁止系统。但我不确定我是否正确设置了 roblox 端,或者我是否正确设置了服务器端。这是我的代码:

This is running within Custom Adonis Admin System这是在自定义 Adonis 管理系统中运行的

SendWebBan = function(plr,UserID,Username,Agent,Comment)
            local http = game:GetService("HttpService")
            local Reason = "Banned in game by AAC Admin."
            local url = "URL HERE/gamebanlistener.php"
            local data = {
                ["UserID"] = UserID,
                ["Username"] = Username,
                ["Agent"] = Agent,
                ["Reason"] = Reason,
                ["Comment"] = Comment
            }

            local encode = http:JSONEncode(data)


            local reponse,err = pcall(function()
                http:PostAsync(url,encode)
            end)

            print(reponse)

            if reponse == "false" then
                Remote.MakeGui(plr,"Notification",{
                    Title = "Failed";
                    Message = "Ban Failed";
                    Duration = 10;
                })
            elseif reponse == "true" then
                Remote.MakeGui(plr,"Notification",{
                    Title = "Success";
                    Message = "Ban Successful";
                    Duration = 10;
                })
            else
                Remote.MakeGui(plr,"Notification",{
                    Title = "Failed";
                    Message = "Ban Failed";
                    Duration = 10;
                })
            end
        end;
<?php
    $ServerKey = "key here";
    $APIKey = $_POST["key"];
    $Data = json_decode($_POST['data']);
    $UserID = $Data.UserID;
    $Username = $Data.Username;
    $Agent = $Data.Agent;
    $Reason = $Data.Reason;
    $Comment = $Data.Comment;

    $Date = date("d/m/Y");
    if($APIKey == $ServerKey){
       //$conn = mysqli_connect($host,$dbuser,$dbpassword,$db);
        $Connection = mysqli_connect(sql credentials here);

    if($Connection->connect_errno)
    {
        echo('{"Status":"'. $Connection->connect_errno . '"}');
    }else {
        $BlacklResult = $Connection->query("SELECT * FROM bans"); //Blacklist is the name of my Bans database

        if($BlacklResult->num_rows > 0)
        {
            while($Row = $BlacklResult->fetch_assoc()){
                if($Row != null){
                    if($Row["UserID"] == $UserID){
                     echo 'Status:false';
                     return;
                    }else{
                     $Connection->query("INSERT INTO bans(UserID, Username, Agent, BanReason, BanComment,DateOfBan) VALUES(".$UserID.",".$Username.",".$Agent.",".$Reason.",".$Comment.",".$Date.");");
                     echo 'Status:true';
                    }
                };
            };
        };

        };
    }else {
        echo('{"Status":"API KEY"}');
    };
?>

When you pcall a function, the tuple that is returned is a bool followed by the result of the function.当您调用 function 时,返回的元组是一个布尔值,后跟 function 的结果。 If the function throws an error, then result is replaced with the error string.如果 function 抛出错误,则将 result 替换为错误字符串。 Try making this change:尝试进行此更改:

local success, result = pcall(function()
    return http:PostAsync(url,encode)
end)

print(success, result)

if success then
    -- request went through, parse the response from your endpoint
else
    -- parse the http error
end

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

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