Recording a JSON POST to File Using PHP

前端 未结 3 407
遥遥无期
遥遥无期 2020-12-11 09:13

I\'m trying to record data that is being posted to my server to a text file. An example of the data that is being sent to my server is located here:

http://dev.data

3条回答
  •  自闭症患者
    2020-12-11 09:23

    To bypass the problem I used JSON.stringify.

    'formSave' : function(project){
        var s = {
            "name": project.name,
            "data": JSON.stringify(project.data)
        };
    
        $.post("sdcform/formSave/" + project.name, s);
    }
    

    The 'project' object contains the keys 'name' and 'data' and I only wanted to stringify the data part of it.

    This way on the server I can do

    $data = isset($_POST['data']) ? json_decode($_POST['data']): new \stdClass();
    file_put_contents($filename, json_encode($data, JSON_PRETTY_PRINT));
    

    I could store it directly to the file and save both conversion but I wanted to prettyfy it!

    Note: Yes I know! Do not access $_POST directly.

提交回复
热议问题