代码之家  ›  专栏  ›  技术社区  ›  Lieutenant Dan

JSON obj到PHP通过ajax命名,->update或insert in.JSON文件[duplicate]

  •  0
  • Lieutenant Dan  · 技术社区  · 6 年前

    Content-Type: text/xml; charset=UTF-8
    User-Agent: Jakarta Commons-HttpClient/3.1
    Host: =={obfuscated}== 
    Content-Length: 1918
    

    我注册的API正在向我的脚本发布一个JSON对象,我想用PHP解析该对象。从请求头可以看到,有一个很好的大而胖的JSON对象正在等待解析。这看起来很简单,但事实并非如此。

    一开始我试着用 $_POST['json'] 或者只是 $_POST 但由于数据不在数组中,我不太确定如何像那样访问它。

    我试过用 file_get_contents('php://input') fopen('php://input', 'r') 有和没有 json_decode() 但运气不好。我不能用 http_get_request_body()

    有没有其他方法可以与我丢失的POST-ed-JSON对象交互?谢谢!

    0 回复  |  直到 15 年前
        1
  •  169
  •   meilke    9 年前

    感谢其他人的投入。原来我只是需要

    $inputJSON = file_get_contents('php://input');
    $input = json_decode($inputJSON, TRUE); //convert JSON into array
    

    其中第二个参数 json_decode

    希望这对其他人有帮助!

        2
  •  3
  •   Gerson E. Aguirre    7 年前

    即使下面的方法有效。

    $inputJSON = file_get_contents('php://input');
    

    如果要继续使用$\u POST,请将数据作为FormData发送

    var fd = new FormData();
    fd.append('key', 'value');
    return axios.post('url', fd)
    
    推荐文章