Can't write file with HttpRequest
Asked Answered
C

3

0

Hi!

I try to read and write a simple textfile from a server. Reading works, but I can't get it to write. The result in OnWriteRequestResult is a success, but it doesn't add the line to the file. Does someone know why?

using Godot;
using System;

public partial class TopListController : Node2D
{
	private const string wUrl = "https://myserver.com/server/w_toplist.php";
	private const string rUrl = "https://myserver.com/server/r_toplist.php";

	public override void _Ready()
	{
		string test = "Name: " + "name" + "\n" + "Mail: " + "email" + "\n" + "Score: " + "60000";
		writeToplist(test);
        readToplist();
		
	}

	public void writeToplist(string data) {
		HttpRequest request = new HttpRequest();
		AddChild(request);
		request.RequestCompleted += (result, response_code, headers, body) => { OnWriteRequestCompleted(result, response_code, headers, body); };
		request.Request(wUrl, new string[] {"toplist_data=" + data}, HttpClient.Method.Post);
	}

	public void readToplist() {
		HttpRequest request = new HttpRequest();
		AddChild(request);
		request.RequestCompleted += (result, response_code, headers, body) => { OnReadRequestCompleted(result, response_code, headers, body); };
		request.Request(rUrl);
	}

	private void OnReadRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
    {
        if (result != (int)HttpRequest.Result.Success)
        {
            // Error handling
            GD.Print("HTTP request failed");
            return;
        }

        string responseString = System.Text.Encoding.UTF8.GetString(body);
        GD.Print(responseString);
    }

	private void OnWriteRequestCompleted(long result, long responseCode, string[] headers, byte[] body)
    {
        if (result == (int)HttpRequest.Result.Success)
        {
            GD.Print("Entry written to server");
            return;
        }

        GD.Print("Failed to write on server");
    }
}

and the php files:

<?php
$toplist_data = $_POST['toplist_data'];
$file = fopen("toplist.txt", "w");
fwrite($file, $toplist_data);
fclose($file);
?>
<?php
$file = fopen("toplist.txt", "r");
$toplist_data = fread($file, filesize("toplist.txt"));
fclose($file);
echo $toplist_data;
?>

Thank you very much in advance!

Cereus answered 7/3, 2023 at 21:53 Comment(0)
M
0

Wow, another PHP guy! 😀

Have you checked the server logs for errors? And check the directory and file permissions to make sure they're writable.

I would add some debug code to the PHP script to dump some variables and see what's happening.

Here's a PHP function I wrote to help me do that:

// Return a variable as a string, using var_dump().
// For diagnostic use.
//   $mixed - Variable to be output.
function var_dump_ret($mixed = null) {
    ob_start();
    var_dump($mixed);
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

Here's an example of using it. It's logging $GLOBALS, but you could replace that by anything.

$log_dir = "{$_SERVER['HOME']}/godot/logs";
$log_file = "{$log_dir}/" . date('Y-m-d'). '.log';
$stuff = var_dump_ret($GLOBALS); 
file_put_contents($log_file, $stuff, FILE_APPEND);
Monosymmetric answered 7/3, 2023 at 22:51 Comment(0)
C
0

Hi Dave!

Thank you for the tips. It's the first time I try to do network stuff with godot and I'm also not very familiar with php. But thanks to the logs I was able to solve it.

I've changed the line in the 'write' php:
$toplist_data = file_get_contents('php://input');

and in c#:
request.Request(wUrl, null, HttpClient.Method.Post, data);

Cereus answered 8/3, 2023 at 1:8 Comment(0)
M
0

Cereus file_get_contents

It's good that you fixed the problem, but I don't understand how file_get_contents() can write to a file. 😕

Monosymmetric answered 8/3, 2023 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.