+1 vote

I have been follow godot documentation to send data to my server. The connection is okay, but the data sent to my php web is always null. Anyone please guides me on this.

my godot codes:

func _make_post_request(url, data_to_send, use_ssl):
   var query = JSON.print(data_to_send)
   var headers = ["Content-Type: application/json"]
   $HTTPRequest.request(url, headers, use_ssl, HTTPClient.METHOD_POST, query)


func _on_Button_pressed():
    _make_post_request("http://localhost/buymall/site/test","Hello World",false)


func _on_HTTPRequest_request_completed(result, response_code, headers, body):
    var json = JSON.parse(body.get_string_from_utf8())
    print(json.result)
    print(result)
    print(response_code)
    print(body.get_string_from_ascii())

my php codes:

public function actiontest()
{
    $value = array(
        'value'=>$_POST
    );
    if(isset($_POST) && $_POST!=null)
    {
        echo json_encode($value);
    }else
    {
        echo "no post value";
    }
}

The outcome when i press POST button is always return "no post value" from my php site. What are the things i did wrong?

in Engine by (71 points)
edited by

2 Answers

0 votes

I was having the same problem while creating a check update mechanic, the case with my code was that var datatosend wasn’t a dict, but a string with the json code and in php I needed to use filegetcontents('php://input') to get the json data instead of using $_POST

Here's the corrected code that works

Gdscript (Godot 3.3.2):

const VERSION= 10
const USE_SSL=true
const CHECK_UPDATE_URL= “https://myurl.com/checkUpdate.php”

var data_to_send = {"version": VERSION,"platform": OS.get_name()}
var query = JSON.print(data_to_send)
var headers = ["Content-Type: application/json"]
httpRequest.request(CHECK_UPDATE_URL, headers, ViewConstants.USE_SSL, HTTPClient.METHOD_POST, query)

In php (works in 7.4) I used the following code to get the 2 variables:
Php:

$jsonInput = file_get_contents('php://input');
$dataInput = json_decode($jsonInput);
if (is_object($dataInput)){
    $versionInput = $dataInput->version;
    $platformInput = $dataInput->platform;
}
by (14 points)
0 votes

Try this:

GDScript:

var Req = $HTTPRequest
var data = "Hello World"
func _do_stuff():
    Req.request("http://localhost/buymall/site/test?data=" + data.percent_encode(), [], false)

PHP:

<?php

if (isset($_GET['data'])) {
    echo "Data Sent";
} else {
    echo "No data sent";
}

?>

But of course, this is only how to do a GET request, sorry

by (193 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.