深圳全飞鸿
标题:
php获取post上来json数据的常规方法
[打印本页]
作者:
zhgc
时间:
2023-11-5 21:17
标题:
php获取post上来json数据的常规方法
<?php
error_reporting(0);
$data=array();
function object_to_array($obj)
{
$_arr= is_object($obj) ? get_object_vars($obj) : $obj;
foreach($_arr as $key=> $val)
{
$val= (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
$arr[$key] = $val;
}
return $arr;
}
$ary=array();
if (empty($_POST)){
$content = file_get_contents('php://input');
$ary = (array)json_decode($content, true);
$data["msg"]="OK1...".var_export($ary,true);
} else {
$ary=object_to_array($_POST);
$data["msg"]="OK2...".var_export($ary,true);
}
header('Content-type: text/json; charset=utf-8');
echo json_encode($data,JSON_UNESCAPED_UNICODE);
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2