微信登陆网页

红太狼 2020-06-22 16:29 1108阅读 0赞

微信开放平台

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

微信登陆网站——Oauth class

  1. <?php
  2. class Oauth{
  3. private $errorMsg;
  4. private $appMsg;
  5. public function __construct(){
  6. $this->errorMsg = array(
  7. "20001" => "<h2>配置文件损坏或无法读取,请重新执行intall</h2>",
  8. "30001" => "<h2>The state does not match. You may be a victim of CSRF.</h2>",
  9. "50001" => "<h2>可能是服务器无法请求https协议</h2>可能未开启curl支持,请尝试开启curl支持,重启web服务器,如果问题仍未解决,请联系我们"
  10. );
  11. $this->appMsg = array(
  12. "appid" => "appid",//微信开放平台申请的网站应用的appid和secret
  13. "secret" => "secret",
  14. "callback" => "http://www.jenny.com/wechatcallback"//回调地址,一定要加http或者https;
  15. );
  16. }
  17. public function wechat_login(){
  18. //-------生成唯一随机串防CSRF攻击
  19. $state = md5(uniqid(rand(), TRUE));
  20. $_SESSION['state'] = $state;
  21. //-------构造请求url
  22. $callback = urlencode($this->appMsg['callback']);
  23. $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";
  24. header("Location:$login_url");
  25. }
  26. public function wechat_callback(){
  27. $state = $_SESSION['state'];
  28. //--------验证state防止CSRF攻击
  29. if($_GET['state'] != $state){
  30. $this->showError("30001");
  31. }
  32. //-------请求参数列表
  33. if(!empty($_GET['code'])){
  34. //dosomething
  35. $code = $_GET['code'];
  36. $curl = curl_init();
  37. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appMsg['appid']."&secret=".$this->appMsg['secret']."&code=".$code."&grant_type=authorization_code";
  38. curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 获取的信息以文件流的形式返回
  41. $result = curl_exec($curl);
  42. curl_close($curl);
  43. if( !empty($result)){
  44. $result_array = json_decode($result,true);
  45. $_SESSION['token'] = $result_array['access_token'];
  46. $_SESSION['openid'] = $result_array['openid'];
  47. header("location:/user/third?provider=wechat");
  48. }else{
  49. header("location:/login");
  50. }
  51. }else{
  52. header("location: /login");
  53. }
  54. }
  55. public function showError($code, $description = '$'){
  56. echo "<meta charset=\"UTF-8\">";
  57. if($description == "$"){
  58. die($this->errorMsg[$code]);
  59. }else{
  60. echo "<h3>error:</h3>$code";
  61. echo "<h3>msg :</h3>$description";
  62. exit();
  63. }
  64. }
  65. }
  66. ?>

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

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

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

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

发表评论

表情:
评论列表 (有 0 条评论,1108人围观)

还没有评论,来说两句吧...

相关阅读

    相关 登陆

    1.需要一个微信测试账号,项目上线时换成正式账号 注册测试帐号只需要一个手机号,并且拥有微信帐号即可,具体流程如下: 打开注册的网址:[http://mp.weixin.q

    相关 登陆和支付

    微信支付方式 1、刷卡支付 刷卡支付是用户展示微信钱包内的“刷卡条码/二维码”给商户系统扫描后直接完成支付的模式。主要应用线下面对面收银的场景。 2、扫码支付 扫

    相关 Java授权登陆

    微信登陆   前提: 微信公众平台:注册微信认证的公众号也就是服务号 ,拥有跟高级权限的微信接口。(注册服务号需要一些企业信息,需自己或者公司解决) 注: 2018年1