脚本宝典收集整理的这篇文章主要介绍了postman 简单的实现接口登陆添加(php 生成token值验证),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
1@H_406_7@ <?PHP
2
3 namespace App\Http\Controllers\Api;
4
5 use Illuminate\Http\Request;
6 use App\Http\Controllers\Controller;
7 use Illuminate\Support\FaCADes\DB;
8
9 class ApiController extends AppController
10 {
11 public function api(Request $request){
12 //接受name,pwd值
13 $name = $request->post(‘name‘);
14 $pwd = $request->post(‘password‘);
15
16 //sql 查询
17 $res = DB::table(‘user‘)->where("user",‘=‘,"$name")->where(‘password‘,"$pwd")->First();
18
19 if ($res){
20 //生成token
21 $num = rand(1000,9999);
22 $tokenName = ‘hello‘;
23 $time = date(‘YmdHis‘,time());
24
25 $arr = array(
26 $num,$tokenName,$time
27 );
28 sort($arr,SORT_STRING);
29
30 $str = implode($arr);
31
32 $strStr = sha1($str);
33
34
35 //判断是否存在文件 或者 当前时间-文件创建时间 如果大于7200 则 重新写入文件
36 if (!file_exists(‘token.txt‘) || time()-filemtime(‘token.txt‘) > 7200){
37
38 file_put_contents(‘token.txt‘,$strStr);
39
40
41 }else{
42 //否之读取文件token
43 $strs = file_get_contents(‘token.txt‘);
44 // 还剩多少秒过期
45 $ri = 7200 - (time()-filemtime(‘token.txt‘));
46 //返回数组值
47 return [
48 ‘code‘ => 200, 49 ‘message‘ => ‘登陆成功‘, 50 ‘token‘ => $strs, 51 ‘gtime‘ => "token" .$ri . "秒后过期"
52
53 ];
54 }
55
56
57
58 }else{
59 //否之登陆失败
60 return [
61 ‘code‘ => 300, 62 ‘message‘ => ‘登陆失败,请重新登陆‘
63
64 ];
65 }
66
67 }
68
69 //此为添加方法
70 public function add(Request $request){
71
72
73 //接收获取的token值
74 $token = $request->post(‘token‘);
75
76
77 //将token值传入getAdd方法进行验证
78 $res = $this->getAdd($token);
79
80
81 //同样接收 对应值
82 $name = $request->post(‘name‘);
83
84 $password = $request->post(‘password‘);
85
86 //转为为数组
87 $arr = [
88 ‘user‘ => $name, 89 ‘password‘ => $password
90 ];
91
92
93 //判断返回值 ture 则进行添加 否之 返回 添加失败
94 if ($res){
95
96 $res1 = DB::table(‘user‘)->insert($arr);
97
98 if ($res1){
99
100 return [
101 ‘code‘ => 200,102 ‘message‘ => ‘添加成功‘,103 ];
104 }else{
105
106 return [
107 ‘code‘ => 300,108 ‘message‘ => ‘添加失败‘
109 ];
110 }
111
112 }else{
113 //token值验证失败 返回对应内容
114 return [
115 ‘code‘ => 400,116 ‘message‘ => ‘token值不正确,请重新获取‘
117 ];
118
119
120 }
121
122
123
124
125
126 }
127 //检测传入token值是否正确
128 public function getAdd($token){
129 //读取本地 文件token值
130 $tokens = file_get_contents(‘token.txt‘);
131
132
133 if ($tokens == $token){
134
135 return 1;
136 }else{
137
138 return false;
139 }
140
141
142 }
143 //自己定义测试方法 当然也可选择 postman进行测试
144 public function postMan(){
145
146 $url = "http://www.laraval2.COM/api/api";
147
148 $urls = "http://www.laraval2.com/api/add";
149
150 $arrLogin = [
151 ‘name‘ => ‘2‘,152 ‘password‘ => ‘2‘,153
154 ];
155
156 $arrAdd = [
157 ‘name‘ => ‘6‘,158 ‘password‘ => ‘2‘,159 ‘token‘ => ‘8e07f3548818817d2cd05e6c7c0fb3c5F17aefcb‘,160
161 ];
162
163 //调用父级控制器的 getCurl方法 发送数据到客户端
164 $res = $this->getCurl($url,‘post‘,$arrLOGin);
165 //接受返回值,并转为数组形式
166 $arr = json_decode($res,true);
167 //打印
168 var_dump($arr);
169
170
171 }
172
173
174 }
对应的父级控制器 AppController.PHP : Curl方法
1 public function getCurl($url,$month=‘post‘,$can=null){ 2
3 $ch = curl_init(); 4
5 curl_setopt($ch,CURLOPT_URL,$url); 6 curl_setopt($ch,CURLOPT_RETURNtransfer,1); 7
8 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); 9
10
11 if ($month==‘post‘) { 12 curl_setopt($ch,CURLOPT_POST,1); 13 curl_setopt($ch,CURLOPT_POSTFIELDS,$can); 14
15 } 16 $data = curl_exec($ch); 17
18
19 curl_close($ch); 20
21 return $data; 22
23
24
25 }
以上是脚本宝典为你收集整理的postman 简单的实现接口登陆添加(php 生成token值验证)全部内容,希望文章能够帮你解决postman 简单的实现接口登陆添加(php 生成token值验证)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。