关于slim的post请求,文章比较少,有也是示例返回一个字符串,当然也有解析的,但基本用的都是下面方式取:
$user = $app->request()->post();
这不是常规标准的数组,当然这并不表示不能解析,用下面代码可以读取:
$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"}]