2018-06-27

php微信扫描登陆


locate是一个框架的跳转方法,参考就行。

<a href="index/wxlogin">微信登陆</a>

/**

    * 微信登录

    */

public function wxlogin(){

$appid=你的appid;

$url="https://xxxxx/index/wxredirecturi";//你接收微信返回数据的路径

$state=asdadadasdaadada;//随机值

$_SESSION['state']=$state;

$wxurl="https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$url&response_type=code&scope=snsapi_login&state=$state#wechat_redirect";

header($this->locate($wxurl));

}

public function wxredirecturi($code,$state){ //接收微信返回数据的方法

if($code&&$state==$_SESSION['state']){

unset($_SESSION['state']);

$user=$this->getwxuserinfo($code); //获取到code之后就能获取到用户数据

if($user=='1'){ //成功

$this->locate(’url‘); //跳到你想要跳到的页面路径

}elseif($user=='2'){ //成功

$this->locate(’url‘); //跳到你想要跳到的页面路径

}

}else{//失败

$this->locate(’url‘); //跳到你想要跳到的页面路径

}

}

/**获取用户信息

    * @param $code

    */

public function getwxuserinfo($code){

$auth=$this->getwxaccestoken($code);

$access_token=$auth[access_token];

$openid=$auth[openid];

$url="https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";

$userinfo=$this-->curlget($url); //get请求

return $userinfo;

}

/**

* get请求

* @param $url

* @return mixed

*/

public function curlget($url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );

    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );

    $response = curl_exec($ch);

    curl_close( $ch );

    $rest = json_decode($response,true);

    return $rest;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容