1

So, my problem is : I have a file (data_test.json) on the web. I wrote and uploaded it myself.

  {
    "a" : 1,
    "b" : 42,
    "c" : 3.14
  }

My local python code in Renpy read it rightly.

    init python:

      def get_stats():
        # Déclaration des variables
        global test_a
        global test_b
        global test_c

        # Importation des modules
        import requests
        import json

        # Atteindre le fichier
        r = requests.get("https://lecot-christophe.com/rpu_json/data_test.json")
        # Aquérir les données sous forme de texte
        r.text

        # Convertir en dictionnaire Python
        var = json.loads(r.text)

        # Boucler pour extraire et distribuer les résultats à travers les variables
        for i in var:
            test_a = var['a']
            test_b = var['b']
            test_c = var['c']

Then the python code makes an update. I think python send rightly the data cause of the response of the requests. post is positive.

      def post_stats(file):
        global test_a
        global test_b
        global test_c
        global send_test

        import requests
        import json

        url = "https://*monsite*/rpu_json/renpy_json_manager.php"

        # Composition des données
        file_name = file+".json"
        aDict = {
            "a":test_a,
            "b":test_b,
            "c":test_c
        }
        json_object = json.dumps(aDict)

        # Adresser le fichier
        data_test = requests.post(url, json=json_object)
        send_test = data_test.status_code

PHP receive the posted data or not, and update the json file.

<?php
$json = $_POST['data'];
$info = json_encode($json);
$file = fopen('data_test.json','w+') or die("File not found");
fwrite($file, $info);
fclose($file);
?>

But the content of data-test.json is now just "null".

null

So... Where did I make a big mistake ?

Chris Charabaruk
  • 4,367
  • 2
  • 30
  • 57
  • Start at the end and work backwards. What's actually in `$_POST['data'];`? Why is `json_decode()` returning NULL but not throwing a warning about it? is your `error_reporting` not turned on/up enough to show you the warning? – Sammitch Mar 19 '22 at 00:21
  • Thanks Sammitch, I take your advice and use about 10 conditionnal request and made a error txt file to report errors. I discover that requests.post never sent any data or no one was received. I used another function of Requests package and pass keys:values into url with get method. Now, php get the current url and convert the array of results into json. And it works ! That's not very clean, i think, but it does the job. – user18507598 Mar 19 '22 at 02:08

0 Answers0