深圳全飞鸿

标题: php获取post上来json数据的常规方法 [打印本页]

作者: zhgc    时间: 2023-11-5 21:17
标题: php获取post上来json数据的常规方法
  1. <?php
  2. error_reporting(0);

  3. $data=array();

  4. function object_to_array($obj)
  5. {
  6.     $_arr= is_object($obj) ? get_object_vars($obj) : $obj;
  7.     foreach($_arr as $key=> $val)
  8.     {
  9.         $val= (is_array($val) || is_object($val)) ?  object_to_array($val) : $val;
  10.         $arr[$key] = $val;
  11.     }
  12.     return $arr;
  13. }
  14. $ary=array();
  15. if (empty($_POST)){
  16.     $content = file_get_contents('php://input');
  17.     $ary    = (array)json_decode($content, true);
  18.         $data["msg"]="OK1...".var_export($ary,true);
  19. } else {
  20.         $ary=object_to_array($_POST);
  21.         $data["msg"]="OK2...".var_export($ary,true);
  22. }  



  23. header('Content-type: text/json; charset=utf-8');
  24. echo  json_encode($data,JSON_UNESCAPED_UNICODE);       
复制代码







欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2