1. 程式人生 > >php 封裝微信自動登入註冊方法基於thinkphp方法【php】

php 封裝微信自動登入註冊方法基於thinkphp方法【php】

首先開啟thinkphp公共function.php檔案

    //獲取伺服器的access_token
function GetSeverToken(){
        // $content=file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_appid').'&secret='.C('WX_appsecret').'');
        $url= 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.C('WX_appid').'&secret='.C('WX_appsecret').'';
        // echo $url;
        $data=posthttps($url);
        $content=explode('access_token":"',$data)[1];
        // 獲取access_token
        $access_token=explode('","',$content)[0];
        return $access_token;
    }

    //請求https
function posthttps($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_HEADER, false);    //表示需要response header
        curl_setopt($curl, CURLOPT_NOBODY, FALSE); //表示需要response body
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//這個是重點。
        $data = curl_exec($curl);
        curl_close($curl);
        // var_dump($data);
        return $data;
    }

//隨機字串
function generate_password( $length = 8 ) { 
 
// 密碼字符集,可任意新增你需要的字元 
$chars ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
$password =''; 
for ( $i = 0; $i < $length; $i++ ) 
{ 
// 這裡提供兩種字元獲取方式 
// 第一種是使用 substr 擷取$chars中的任意一位字元; 
// 第二種是取字元陣列 $chars 的任意元素 
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); 
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
} 
return $password; 
} 

以上三個函式為後面的登入註冊 做準備

自動登入註冊登入函式實

//獲取使用者資訊 並且登入如果沒有則註冊
function getuserinfo(){

        $code=isset($_GET['code'])?$_GET['code']:'0';
        if($code){
          $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('WX_appid').'&secret='.C('WX_appsecret').'&code='.$code.'&grant_type=authorization_code';
          
          $data=posthttps($url);
          $access_token=explode('"access_token":"',$data)[1];
          $access_token=explode('"',$access_token)[0];

          $refresh_token=explode('"refresh_token":"',$data)[1];
          $refresh_token=explode('"',$refresh_token)[0];

          $openid=explode('"openid":"',$data)[1];
          $openid=explode('"',$openid)[0];

          $list=getuser($access_token,$openid,$refresh_token);
          $json=json_decode($list, true);
          // var_dump($json);exit;
          // login($data);
          return  $json;
      }else{
        return 0;
      }

} 

自動登入函式封裝

  //使用者自動註冊登入
//微信註冊登入驗證
  function login($urls){
      $token=cookie('token');
      //測試------------------------------------------------------------------
      $list=M('user')->where('openid="o_DIR010aYD79IWEJdAQudjw19Bs"')->find();
      return $list;exit;
      //測試------------------------------------------------------------------
      if($token){
          // echo 'cookie存在';
          $list=M('user')->where('token="'.$token.'"')->find();
          if(!$list){
            cookie('token',null);
            echo '您已經被擠下線了';
            exit;
          }
          return $list;
          exit;
      }
      $arr=getuserinfo();
      //判斷openid是否存在
      if(!$arr['openid']){
        if($urls){
          header("Location: ".echourl($urls));
        }else{
            header("Location: ".echourl()); 
        }
          exit;
      }

      $list=M('user')->where('openid="'.$arr['openid'].'"')->find();
      if($list['openid']){
          $salt=generate_password(4);
          $token= MD5($arr['openid'].$salt.microtime());
          M('user')->where('openid="'.$arr['openid'].'"')->save(array('token'=>$token));
          // echo '已註冊';
          cookie('token', $token,60*30);
          return $list;exit;

      }else{

        $token= MD5($arr['openid'].$salt.microtime());
        $data=array(
          'openid'=>$arr['openid'],
          'username'=>$arr['nickname'],
          'header'=>$arr['headimgurl'], 
          'token'=>$token
        );
        $id=M('user')->add($data);
        $data['uid']=$id;
        // echo '註冊成功';
        cookie('token', $token,60*30);
        return $data;exit;
      }

    }     
   


tinkphp index控制器index方法獲取使用者名稱 自動驗證自動註冊登入

$user=login();

var_dump($user);