微信登陆网页

来源:互联网 发布:暴风影音 mac 10.6 编辑:程序博客网 时间:2024/04/30 14:15

微信开放平台

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


微信登陆网站——Oauth class

<?phpclass 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 0