服务端编写API总结

1.服务端返回的数据都是最终都是经过json_encode处理的,格式基本上是

$response = [
    'code'=> 200,//状态码
    'msg'=> '请求成功',//提示信息
    'data'=> $data//返回数据
    ];

其中$data可以是数组,如果$data为关联数组或者无序数组这会返回为对象

关联数组
$data = [
    'name'=>'bread',
    'price'=>'8.05',
    'attr'=> '都是感受到三个',
    'date'=> '2018-05-01'
];
无序数组
$data = [
    0=>'test1',1=>'test2',3=>'test3',4=>'test4'
];

是有序数组就返回为数组

$data = ['www.sdsf.com/sdf2334.png','www.sdsf.com/sdf24334.png','www.sdsf.com/df234.png','www.sdsf.com/srw34.png'];

json_encode递归循环格式化也是这个方式
2.$data中千万不要有json_encode的字符串,否则客户端接受时无法还原.就算数据库保存的为json字符串,也应该还原成数组再返回

猜你喜欢

转载自blog.csdn.net/z15818264727/article/details/80312261
今日推荐