How do I use HttpService:PostAsync() to post to a PHP Script

So I have a system used for sending player data to a database so it can be logged.
(All sensitive data has been replaced with other values)

local parameterName = "LOG_VALUE"
for i,v in pairs(service.Players:GetChildren()) do
					if v:FindFirstChild(parameterName) then
						local ExportValue = v:FindFirstChild(parameterName).Value
						local URL = "https://urlhere/api/v1/logger.php"
						
						local Data = {
							["key"] = "key";
							["LogName"] = "PETCM_LOG";
							["Username"] = v.Name;
							["UserID"] = v.UserId;
							["Points"] = ExportValue;
							["Division"] = "PETCM"
						}
						Data = http:JSONEncode(Data)
						local response = http:PostAsync(URL,Data)
						print(response)
					end
				end

And on Server-Side (PHP) I have:

<?php
echo(var_dump($_POST));
if(!isset($_POST['key'])){
 echo('{"Status": "API_KEY"}');
}else{
 $APIKey = "key";
 $PostKey = $_POST['key'];

 if($APIKey != $PostKey){
  echo('{"Status": "API_KEY"}');
 }else{
  $Connection = mysqli_connect("address","username","password","database");
  if($Connection->connect_errno){
   echo('{"Status":"'.$Connection->connect_errno.'"}');
  }else{
   $LogName = $_POST['LogName'];
   $LogUser = $_POST['Username'];
   $LogUserID = $_POST['UserID'];
   $Points = $_POST['Points'];

   $LogTable = $_POST['Division'].'Logs';

   $InsertQuery = $Connection->query("INSERT INTO ".$LogTable."(LogName,LogUser,LogUserId,LogPoints) VALUES(".$LogName.",".$LogUser.",".$LogUserID.",".$Points.");");
   echo('{"Status": "LOGGED"}');
  }
 }
}
?>

I get no errors when running this code, but when I print the response in roblox, the var_dump from PHP shows there’s nothing in the $_POST data. Did I use incorrect methods on PHP or roblox, or where does my issue lie?

1 Like

If you are trying to return data from your php script into HttpService GetAsync, I don’t think that’s possible. Why are you using FindFirstChild(“ParameterName”) when it definitely exists?

1 Like

When you post JSON content, the data will not be stored in $_POST. Use file_get_contents("php://input"); instead.

1 Like