Okay, today a small brainstorming on how to send/recieve JSON data from Godot4 and php servers

Okay, today a small brainstorming on how to send/recieve JSON data from Godot4 and php servers
<?php
// Sample array
$data = array("a" => "Apple", "b" => "Ball", "c" => "Cat");

header("Content-Type: application/json");
echo(json_encode($data));
?>

extends Node2D


func _ready():
    send_to_server()

func _on_request_completed(result, response_code, headers, body):
    var json = JSON.parse_string(body.get_string_from_utf8())
    print(json["a"])

func _on_request_completed2(result, response_code, headers, body):
    print(body.get_string_from_utf8())

func get_from_server():
    var hreq:HTTPRequest=HTTPRequest.new()
    add_child(hreq)
    var headers = ["Content-Type: application/json"]
    hreq.request_completed.connect(_on_request_completed)
    hreq.request("https://hrubos.tech/commie/post.php",headers, HTTPClient.METHOD_POST)

func send_to_server():
    var hreq:HTTPRequest=HTTPRequest.new()
    add_child(hreq)
    var headers = ["Content-Type: application/json"]
    hreq.request_completed.connect(_on_request_completed2)
    var data_to_send = "{\"w\":\"x\"}"
    var json = JSON.stringify(data_to_send)
    hreq.request("https://hrubos.tech/commie/post2.php",headers, HTTPClient.METHOD_POST,json)


Author: AarNoma

The first Slovak cyborg 1 system

Comments “Okay, today a small brainstorming on how to send/recieve JSON data from Godot4 and php servers”