douqian1975 2012-11-19 03:28
浏览 29
已采纳

PHP - 收到POST json的麻烦

I am writing some simple PHP to receive a POST from another system with a JSON payload. That request is Content-Type: application/json.

I'm simply attempting to dump the json into a txt file as a starting point, but that isn't happening. Here's the PHP file, any suggestions/corrections would be much appreciated!

<?php
    ini_set("display_errors", "On");
    session_start();

    $raw_json = file_get_contents('php://input');
    $cooked_json = json_decode($raw_json);


    $myfile = fopen('/home/wgordon/log.txt','a');
    $fp = fwrite($myfile, $cooked_json);
    fclose($myfile);

?>

  • 写回答

1条回答 默认 最新

  • duanlinma5885 2012-11-19 03:36
    关注

    From the look of it, the initial value of $raw_json would be a string. When you try to access it as an array, you're only going to get a smaller string back. When you pass that to json_decode, it's probably going to return null.

    You need to inspect that first value of $raw_json. If it is coming in as a query string of name/value pairs, you'll need to decode it (using split() or something similar) before you can treat it like an array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序