微信登陆网页

1 篇文章 0 订阅
1 篇文章 0 订阅

微信开放平台

https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN


微信登陆网站——Oauth class

<?php
class Oauth{
    private $errorMsg;
    private $appMsg;

	public function __construct(){	
		$this->errorMsg = array(
            "20001" => "<h2>配置文件损坏或无法读取,请重新执行intall</h2>",
            "30001" => "<h2>The state does not match. You may be a victim of CSRF.</h2>",
            "50001" => "<h2>可能是服务器无法请求https协议</h2>可能未开启curl支持,请尝试开启curl支持,重启web服务器,如果问题仍未解决,请联系我们"
            );
		$this->appMsg = array(
            "appid" => "appid",//微信开放平台申请的网站应用的appid和secret
            "secret" => "secret",
            "callback" => "http://www.jenny.com/wechatcallback"//回调地址,一定要加http或者https;
            );
		
	}
			
	public function wechat_login(){

		//-------生成唯一随机串防CSRF攻击
		$state = md5(uniqid(rand(), TRUE));
		$_SESSION['state'] = $state;

		//-------构造请求url
		$callback = urlencode($this->appMsg['callback']);
		$login_url =  "https://open.weixin.qq.com/connect/qrconnect?appid=".$this->appMsg['appid'] ."&redirect_uri=".$callback."&response_type=code&scope=snsapi_login&state=".$state."#wechat_redirect";

		header("Location:$login_url");
	}
		
	public function wechat_callback(){
		$state = $_SESSION['state'];

        //--------验证state防止CSRF攻击
        if($_GET['state'] != $state){
            $this->showError("30001");
        }

        //-------请求参数列表
		if(!empty($_GET['code'])){
			//dosomething
			$code = $_GET['code'];
			$curl = curl_init();
			$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appMsg['appid']."&secret=".$this->appMsg['secret']."&code=".$code."&grant_type=authorization_code";
			
			curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址	
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查     
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 获取的信息以文件流的形式返回
			$result = curl_exec($curl);
			curl_close($curl);
				
			if( !empty($result)){
				$result_array = json_decode($result,true);
				$_SESSION['token'] = $result_array['access_token'];
				$_SESSION['openid'] = $result_array['openid'];
				header("location:/user/third?provider=wechat");
			}else{
				header("location:/login");
			}
		}else{
			header("location: /login");
		}
	}
	
	 public function showError($code, $description = '$'){
        echo "<meta charset=\"UTF-8\">";
        if($description == "$"){
            die($this->errorMsg[$code]);
        }else{
            echo "<h3>error:</h3>$code";
            echo "<h3>msg  :</h3>$description";
            exit(); 
        }
    }		
	
}
?>

微信登陆网站——callback控制文件

	require_once($this->lib_wechatoauth_path . 'wechatoauth.php');
	$oauth = new Oauth();
	$wechat_token = $oauth->wechat_callback();

微信登陆网站——login控制文件

        require_once($this->lib_wechatoauth_path . 'wechatoauth.php');
	$oauth = new Oauth();
	$wechat_token = $oauth->wechat_login();


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值