深圳全飞鸿

标题: jsonAPI的源码分析 [打印本页]

作者: admin    时间: 2019-7-13 22:57
标题: jsonAPI的源码分析
jsonAPI的源码分析


处理数据进入的处理:

版本一:
  1. //提取post过来的参数   V1
  2. foreach($_POST as $k=>$v)
  3. {
  4.    $ret[strtolower($k)]=$v;
  5. }
复制代码


版本二:
  1. //提取post过来的参数 V2版
  2. function object_to_array($obj)
  3. {
  4.     $_arr= is_object($obj) ? get_object_vars($obj) : $obj;
  5.     foreach($_arr as $key=> $val)
  6.     {
  7.         $val= (is_array($val) || is_object($val)) ?  object_to_array($val) : $val;
  8.         $arr[$key] = $val;
  9.     }
  10.     return $arr;
  11. }
  12. //提取post过来的参数
  13. $ary=object_to_array($_POST);
  14. foreach($ary as $k=>$v)
  15. {
  16.    if(is_array($v))
  17.    {
  18.        foreach($v as $k1=>$v1)
  19.        {
  20.            $ret[strtolower($k1)]=$v1;
  21.         }
  22.    }
  23.    else
  24.    {
  25.       $ret[strtolower($k)]=$v;
  26.    }
  27. }
复制代码


版本三:
  1. function object_to_array($obj)
  2. {
  3.     $_arr= is_object($obj) ? get_object_vars($obj) : $obj;
  4.     foreach($_arr as $key=> $val)
  5.     {
  6.         $val= (is_array($val) || is_object($val)) ?  object_to_array($val) : $val;
  7.         $arr[$key] = $val;
  8.     }
  9.     return $arr;
  10. }
  11. $ary=array();
  12. if (empty($_POST)){
  13.     $content = file_get_contents('php://input');
  14.     $ary    = (array)json_decode($content, true);
  15. } else {
  16.         $ary=object_to_array($_POST);
  17. }  
  18. foreach($ary as $k=>$v)
  19. {
  20.    if(is_array($v))
  21.    {
  22.        foreach($v as $k1=>$v1)
  23.        {
  24.            $ret[strtolower($k1)]=$v1;
  25.         }
  26.    }
  27.    else
  28.    {
  29.       $ret[strtolower($k)]=$v;
  30.    }
  31. }
复制代码






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