zoukankan      html  css  js  c++  java
  • CURL 调用登录接口并且携带Token

    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();
    }



  • 相关阅读:
    Math.pow
    css3正方体
    制作一个百度换肤效果
    排他思想
    js栈和堆的区别
    js创建对象的几种方式(工厂模式、构造函数模式、原型模式)
    短网址
    this
    作用域
    JS 函数基础
  • 原文地址:https://www.cnblogs.com/chaihtml/p/10494163.html
Copyright © 2011-2022 走看看