curl页面:
<?php namespace frontendcontrollers; use yiiaseController; use Yii; class NewController extends Controller{ public $result=array( 'code'=>0, 'data'=>'', 'error'=>'' ); public function actionIndex(){ $user=Yii::$app->request->get('user');//获取用户名 $pwd=Yii::$app->request->get('pwd');//获取密码 $token=file_get_contents('./user.txt');//生成token if(empty($user)){ return 1004; } if(empty($pwd)){ return 1005; } $url='http://www.xyii.com/v1/new/login';//登录接口地址 $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_POST,TRUE); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');//出现身份认证问题时加入
curl_setopt($ch,CURLOPT_HEADER,FALSE); curl_setopt($ch,CURLOPT_POSTFIELDS,array('user'=>$user,'pwd'=>$pwd,'token'=>$token)); $data=curl_exec($ch); curl_close($ch); $data=json_decode($data); $this->result['data']=$data; return json_encode($this->result); } }
//登录接口 控制器层
$user=Yii::$app->request->post('user'); $pwd=Yii::$app->request->post('pwd'); $id=Yii::$app->request->post('id'); $token=Yii::$app->request->post('token'); if($id<1){ return 1000; } if(empty($user)){ return 1001; } if(empty($pwd)){ return 1002; } if(empty($token)){ return 1003; } $params=array( 'user'=>$user, 'pwd'=>$pwd ); $rows=User::getOne($id); $this->result['data']=$rows; return $this->result;
//模型层
public static $_tbl='user'; public static function getOne($id){ $db=Yii::$app->db->createCommand("SELECT `id`,`user`,`pwd` FROM".self::$_tbl); $query=$db->bindValue('id',$id)->queryOne(); if(is_array($query)){ return $query; } return array(); }