slim的httppost数据的解析

关于slim的post请求,文章比较少,有也是示例返回一个字符串,当然也有解析的,但基本用的都是下面方式取:

$user = $app->request()->post();

但这种方式取,用var_dump($user);输出,我遇到的却是这样的结果:


这不是常规标准的数组,当然这并不表示不能解析,用下面代码可以读取:

$keys=array_keys($user);
$value=$keys[0];
然而这确实多此一步了,理论上应该直接是json才对啊,摸索一下,其实用这样就好了:

$postvalue = $app->request()->getBody();
如果post这个过去:

{"id":0,"imei":"10000","alias":"000","name":"wood","location":"guangzhou"}

解析代码这样写:

$postvalue = $app->request()->getBody();
$value=json_decode($postvalue,true);    //反序列化
$result = $users->insert($value);    //数据库插入操作
如果post这个:

[{"id":0,"imei":"10000","alias":"000","name":"wood","location":"guangzhou"},{"id":1,"imei":"10000","alias":"000","name":"wood","location":"guangzhou"}]

就这样解析:

$postvalue = $app->request()->getBody();
$value=json_decode($postvalue,true);    //反序列化
foreach($value as $vue){
$result = $users->insert($vue);    //数据库插入操作
}
这样就变成两个方法了,既然这样,干脆统一用第二种,哪怕只有一行数据,也这样post就好了:

[{"id":0,"imei":"10000","alias":"000","name":"wood","location":"guangzhou"}]





你可能感兴趣的:(slim的httppost数据的解析)