Zenovain
(Zenovain)
October 16, 2021, 10:17am
#1
I’m trying to send data to my php server but post data doesn’t show up in the $_POST[‘user’] variable. It would always return ‘failure ’.
ROBLOX SIDE:
local HttpService = game:GetService("HttpService")
local URL = "https://phpwebsite/listener.php"
data = {
['user'] = "bob"
}
jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationUrlEncoded, false)
jsonData = HttpService:JSONDecode(response)
print(jsonData.result)
PHP SIDE:
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
echo "This API is for Post Method.";
}
else{
if(ISSET($_POST['user'])){
$result = $_POST['user'];
echo json_encode(['result' => $result]);
}
else{
echo json_encode(['result' => 'failure']);
}
}
?>
Diltz_3
(Diltz)
October 16, 2021, 10:31am
#2
The content-type of your request is ApplicationUrlEncoded when it should be ApplicationJson, because you send JSON to server
If I’m right it should be:
local HttpService = game:GetService("HttpService")
local URL = "https://phpwebsite/listener.php"
data = {
['user'] = "bob"
}
jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationJson, false)
jsonData = HttpService:JSONDecode(response)
print(jsonData.result)
Also this should be asked in scripting-support, not dev discussion
Zenovain
(Zenovain)
October 16, 2021, 10:44am
#3
Sorry, I’m pretty new to the website.
I tried ApplicationJson as the content-type but it still results in ‘failure’.
I read from other posts that I should use ApplicationUrlEncoded which didn’t work so here I am stuck at this problem.
Diltz_3
(Diltz)
October 16, 2021, 11:06am
#4
you can try debug it by using echo
in php to see what server has request body
Zenovain
(Zenovain)
October 16, 2021, 11:10am
#5
How do I do that?
because what I’m doing is I’m sending a json from roblox to my php server and then printing the ‘result’ on roblox studio?
Diltz_3
(Diltz)
October 16, 2021, 11:11am
#6
no i mean php has function
echo var_dump($_POST);
Diltz_3
(Diltz)
October 16, 2021, 11:13am
#7
then check result on roblox 3921390
Diltz_3
(Diltz)
October 16, 2021, 11:17am
#9
yes, but why there no any results, what is ur current php code now
Zenovain
(Zenovain)
October 16, 2021, 11:21am
#10
Oops my bad, I did it wrong.
Current PHP Code:
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
echo "This API is for Post Method.";
}
else{
$result = $_POST['user'];
var_dump($result);
}
?>
Current Roblox Code
local HttpService = game:GetService("HttpService")
local URL = "https://website/listening.php"
data = {
["user"] = "Zenovain"
}
jsonData = HttpService:JSONEncode(data)
local response = HttpService:PostAsync(URL, jsonData, Enum.HttpContentType.ApplicationJson, false)
print(response)
CURRENT ERROR IN STUDIO
Diltz_3
(Diltz)
October 16, 2021, 11:24am
#11
can u try to var_dump $_POST, not $_POST[user]
Zenovain
(Zenovain)
October 16, 2021, 11:25am
#12
Php Code
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
echo "This API is for Post Method.";
}
else{
$result = $_POST;
var_dump($result);
}
?>