//持续更新中。。。
1.RESTful架构:每一个网址代表一个资源,相比较于传统的路由规则,rest不能有动词,所用的名词与数据库表名对应。
2.服务端返回的数据类型为json格式
//用curl函数模拟post提交
//$post_data 传入数据应该包含但前时间(用于制作token),app_key /app_id.
//$token=MD5(MD5(键值键值).MD5(标识))
2.服务端响应<?php/**
* Created by PhpStorm. * User: THINK * Date: 2017/9/25 * Time: 10:43 */ namespace Home\Controller; use Think\Controller\RestController; class UserController extends RestController { public function lists(){ //获取post提交信息 //生成token // 与传入token对比,不符合返回报错信息//模拟post.get提交 switch($this->_method){ case ‘get‘: echo ‘check all user‘; break; case ‘post‘: if(time()-I(‘post.timestamp‘) > 5){ $data = array( ‘code‘=>‘302‘, ‘mes‘=>‘超时‘, ‘datas‘=>array() ); echo json_encode($data); exit; } if(I(‘post.app_key‘)==‘lol‘){ $data = array( ‘code‘=>‘200‘, ‘mes‘=>‘信息调用成功‘, ‘datas‘=>array( array(‘username‘=>‘1‘, ‘password‘=>‘123‘), array(‘username‘=>‘2‘, ‘password‘=>‘123‘), array(‘username‘=>‘3‘, ‘password‘=>‘123‘), ) ); }else{ $data = array( ‘code‘=>‘301‘, ‘mes‘=>‘参数不正确‘, ‘datas‘=>array() ); } echo json_encode($data); break; } } //模拟put和delete提交 public function show(){ switch($this->_method){ case ‘get‘: echo ‘check id=‘,I(‘get.id‘),‘ user‘; break; case ‘put‘: echo "update id=",I(‘put.id‘)," user"; break; case ‘delete‘: echo "delete id=",I(‘delete.id‘)," user"; break; } } }
时间: 2024-10-10 19:54:55