如何使用微信获取openid的静默及非静默

 5286

不知道你们有没有用过微信的openid,但是今天小编要带大家一起了解一个如何使用PHP,获取到微信openid的静默非静默,有需要的小伙伴可以参考参考。


如何使用微信获取openid的静默及非静默


  1. <?php
  2.  
  3. /*
  4. 需要的微信公众号配置信息
  5. APPID     : 绑定支付的APPID
  6. APPSECRET : 公众帐号secert
  7. */
  8. class Index {
  9.     // 配置账号信息
  10.     private $wxPayConfig = array ();
  11.     protected function _initialize() {
  12.         // 如果有继承的父类,则需先访问父类构造方法
  13.         // parent::_initialize ();
  14.          
  15.         // APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
  16.         $this->wxPayConfig ['APPID']     = 'wx426b3015555a46be';
  17.         // APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置)
  18.         $this->wxPayConfig ['APPSECRET'] = '7813490da6f1265e4901ffb80afaa36f';
  19.          
  20.         /**
  21.          * 这里设置代理机器,只有需要代理的时候才设置,不需要代理,则设置为0.0.0.0和0
  22.          * 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
  23.          * 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
  24.          */
  25.         $this->wxPayConfig ['CURL_PROXY_HOST'] = "0.0.0.0"; // "10.152.18.220";
  26.         $this->wxPayConfig ['CURL_PROXY_PORT'] = 0;
  27.     }
  28.      
  29.     // 通过code获得openid
  30.     public function getOpenid() {
  31.          
  32.         //snsapi_base     (未关注,不弹出授权页面,直接跳转,只能获取用户openid)
  33.         //snsapi_userinfo (弹出授权页面,即使在未关注的情况下,只要用户授权,也能获取其信息,可通过openid拿到昵称、性别、所在地等)
  34.         $scope = "snsapi_base";//这里使用静默授权
  35.          
  36.         // 判断如果没有传 code 则先获取 code
  37.         if (! isset ( $_GET ['code'] )) {
  38.             // 获取当前打开页面的完整访问路径
  39.             $baseUrl = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'] . $_SERVER ['QUERY_STRING'];
  40.             $baseUrl = urlencode ( $baseUrl );
  41.             //配置获取code 路径的相关参数
  42.             $paramsArr                   = array();
  43.             $paramsArr ["appid"]         = $this->wxPayConfig ['APPID'];
  44.             $paramsArr ["redirect_uri"]  = "$baseUrl";
  45.             $paramsArr ["response_type"] = "code";
  46.              
  47.             //scope 应用授权作用域
  48.             $paramsArr ["scope"]         = $scope;
  49.              
  50.             //state 非必须参数,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
  51.             //结尾 #wechat_redirect 必须有,且该字符串前面不能带 & 所以连接到最后一个参数值后面,无论直接打开还是做页面302重定向时候,必须带此参数
  52.             $paramsArr ["state"]         = "STATE" . "#wechat_redirect";
  53.             //将数组变成参数url
  54.             $paramsUrl = $this->arrToParams ( $paramsArr );
  55.             //构造获取code的访问路径,访问后,微信服务器会再跳回$baseUrl指定的页面(这里当前页),并返回 code 码
  56.             $codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?" . $paramsUrl;
  57.             Header ( "Location: $codeUrl" );
  58.             exit ();
  59.         } else {
  60.             // 获取code码,以获取openid
  61.             $code = $_GET ['code'];
  62.             //配置获取 openid 路径的相关参数
  63.             $paramsArr                = array();
  64.             $paramsArr ["appid"]      = $this->wxPayConfig ['APPID'];
  65.             $paramsArr ["secret"]     = $this->wxPayConfig ['APPSECRET'];
  66.             $paramsArr ["code"]       = $code;
  67.             $paramsArr ["grant_type"] = "authorization_code";
  68.             $paramsUrl = $this->arrToParams ( $paramsArr );
  69.             $openidUrl =  "https://api.weixin.qq.com/sns/oauth2/access_token?" . $paramsUrl;
  70.              
  71.              
  72.             $data = $this->httpCurl ( $openidUrl );
  73.             $openid = $data ['openid'];
  74.             if($scope == 'snsapi_userinfo'){
  75.                 //如果是显示授权,即需要用户点击授权,则还会返回 access_token 通过 access_token 及 openid 通过 echo file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN");即可获取打印用户信息,或者用这里的curl方法httpCurl(https://api.weixin.qq.com/sns/userinfo?access_token=...)获取亦可
  76.                 $access_token = $data['access_token'];
  77.             }
  78.             return $openid;
  79.         }
  80.     }
  81.  
  82.      
  83.     /**
  84.      *
  85.      * 通过code从工作平台获取openid机器access_token
  86.      *
  87.      * @param string $code
  88.      *            微信跳转回来带上的code
  89.      *            
  90.      * @return openid
  91.      */
  92.     public function httpCurl($url) {
  93.         // 初始化curl
  94.         $ch = curl_init ();
  95.         // 设置30秒超时
  96.         curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
  97.         curl_setopt ( $ch, CURLOPT_URL, $url );
  98.         curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
  99.         curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
  100.         curl_setopt ( $ch, CURLOPT_HEADER, FALSE );
  101.         curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
  102.         if ($this->wxPayConfig ['CURL_PROXY_HOST'] != "0.0.0.0" && $this->wxPayConfig ['CURL_PROXY_PORT'] != 0) {
  103.             curl_setopt ( $ch, CURLOPT_PROXY, $this->wxPayConfig ['CURL_PROXY_HOST'] );
  104.             curl_setopt ( $ch, CURLOPT_PROXYPORT, $this->wxPayConfig ['CURL_PROXY_PORT'] );
  105.         }
  106.         // 运行curl,结果以jason形式返回
  107.         $res = curl_exec ( $ch );
  108.         curl_close ( $ch );
  109.         // 取出openid
  110.         $data = json_decode ( $res, true );
  111.         return data;
  112.     }
  113.      
  114.     /**
  115.      *
  116.      * 拼接签名字符串
  117.      *
  118.      * @param array $paramsArr            
  119.      *
  120.      * @return 返回已经拼接好的字符串
  121.      */
  122.     private function arrToParams($paramsArr) {
  123.         $buff = "";
  124.         foreach ( $paramsArr as $k => $v ) {
  125.             if ($k != "sign") {
  126.                 $buff .= $k . "=" . $v . "&";
  127.             }
  128.         }
  129.          
  130.         $buff = trim ( $buff, "&" );
  131.         return $buff;
  132.     }
  133. }


本文网址:https://www.zztuku.com/detail-9023.html
站长图库 - 如何使用微信获取openid的静默及非静默
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐