postman 簡單的實現接口登錄添加(php 生成token值驗證)

  1 <?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方法php

 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        }
相關文章
相關標籤/搜索