How to use WeChat to get openid's silent and non-silent information

醉折花枝作酒筹
Release: 2023-03-11 15:36:01
forward
2620 people have browsed it

I don’t know if you have ever used WeChat’s openid, but today I want to take you through how to use PHP to obtain the silent and non-silent versions of WeChat openid. Friends in need can refer to it.

How to use WeChat to get openid's silent and non-silent information

<?php

/*
需要的微信公众号配置信息
APPID     : 绑定支付的APPID
APPSECRET : 公众帐号secert
*/
class Index {
    // 配置账号信息
    private $wxPayConfig = array ();
    protected function _initialize() {
        // 如果有继承的父类,则需先访问父类构造方法
        // parent::_initialize ();
        
        // APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
        $this->wxPayConfig [&#39;APPID&#39;]     = &#39;wx426b3015555a46be&#39;;
        // APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置)
        $this->wxPayConfig [&#39;APPSECRET&#39;] = &#39;7813490da6f1265e4901ffb80afaa36f&#39;;
        
        /**
         * 这里设置代理机器,只有需要代理的时候才设置,不需要代理,则设置为0.0.0.0和0
         * 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
         * 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
         */
        $this->wxPayConfig [&#39;CURL_PROXY_HOST&#39;] = "0.0.0.0"; // "10.152.18.220";
        $this->wxPayConfig [&#39;CURL_PROXY_PORT&#39;] = 0;
    }
    
    // 通过code获得openid
    public function getOpenid() {
        
        //snsapi_base     (未关注,不弹出授权页面,直接跳转,只能获取用户openid)
        //snsapi_userinfo (弹出授权页面,即使在未关注的情况下,只要用户授权,也能获取其信息,可通过openid拿到昵称、性别、所在地等)
        $scope = "snsapi_base";//这里使用静默授权
        
        // 判断如果没有传 code 则先获取 code
        if (! isset ( $_GET [&#39;code&#39;] )) {
            // 获取当前打开页面的完整访问路径
            $baseUrl = &#39;http://&#39; . $_SERVER [&#39;HTTP_HOST&#39;] . $_SERVER [&#39;REQUEST_URI&#39;] . $_SERVER [&#39;QUERY_STRING&#39;];
            $baseUrl = urlencode ( $baseUrl );
            //配置获取code 路径的相关参数
            $paramsArr                   = array();
            $paramsArr ["appid"]         = $this->wxPayConfig [&#39;APPID&#39;];
            $paramsArr ["redirect_uri"]  = "$baseUrl";
            $paramsArr ["response_type"] = "code";
            
            //scope 应用授权作用域
            $paramsArr ["scope"]         = $scope;
            
            //state 非必须参数,重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值,最多128字节
            //结尾 #wechat_redirect 必须有,且该字符串前面不能带 & 所以连接到最后一个参数值后面,无论直接打开还是做页面302重定向时候,必须带此参数
            $paramsArr ["state"]         = "STATE" . "#wechat_redirect";
            //将数组变成参数url
            $paramsUrl = $this->arrToParams ( $paramsArr );
            //构造获取code的访问路径,访问后,微信服务器会再跳回$baseUrl指定的页面(这里当前页),并返回 code 码
            $codeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?" . $paramsUrl;
            Header ( "Location: $codeUrl" );
            exit ();
        } else {
            // 获取code码,以获取openid
            $code = $_GET [&#39;code&#39;];
            //配置获取 openid 路径的相关参数
            $paramsArr                = array();
            $paramsArr ["appid"]      = $this->wxPayConfig [&#39;APPID&#39;];
            $paramsArr ["secret"]     = $this->wxPayConfig [&#39;APPSECRET&#39;];
            $paramsArr ["code"]       = $code;
            $paramsArr ["grant_type"] = "authorization_code";
            $paramsUrl = $this->arrToParams ( $paramsArr );
            $openidUrl =  "https://api.weixin.qq.com/sns/oauth2/access_token?" . $paramsUrl;
            
            
            $data = $this->httpCurl ( $openidUrl );
            $openid = $data [&#39;openid&#39;];
            if($scope == &#39;snsapi_userinfo&#39;){
                //如果是显示授权,即需要用户点击授权,则还会返回 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=...)获取亦可
                $access_token = $data[&#39;access_token&#39;];
            }
            return $openid;
        }
    }

    
    /**
     *
     * 通过code从工作平台获取openid机器access_token
     *
     * @param string $code
     *            微信跳转回来带上的code
     *            
     * @return openid
     */
    public function httpCurl($url) {
        // 初始化curl
        $ch = curl_init ();
        // 设置30秒超时
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 30 );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt ( $ch, CURLOPT_HEADER, FALSE );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, TRUE );
        if ($this->wxPayConfig [&#39;CURL_PROXY_HOST&#39;] != "0.0.0.0" && $this->wxPayConfig [&#39;CURL_PROXY_PORT&#39;] != 0) {
            curl_setopt ( $ch, CURLOPT_PROXY, $this->wxPayConfig [&#39;CURL_PROXY_HOST&#39;] );
            curl_setopt ( $ch, CURLOPT_PROXYPORT, $this->wxPayConfig [&#39;CURL_PROXY_PORT&#39;] );
        }
        // 运行curl,结果以jason形式返回
        $res = curl_exec ( $ch );
        curl_close ( $ch );
        // 取出openid
        $data = json_decode ( $res, true );
        return data;
    }
    
    /**
     *
     * 拼接签名字符串
     *
     * @param array $paramsArr            
     *
     * @return 返回已经拼接好的字符串
     */
    private function arrToParams($paramsArr) {
        $buff = "";
        foreach ( $paramsArr as $k => $v ) {
            if ($k != "sign") {
                $buff .= $k . "=" . $v . "&";
            }
        }
        
        $buff = trim ( $buff, "&" );
        return $buff;
    }
}
Copy after login

Recommended learning: php video tutorial

The above is the detailed content of How to use WeChat to get openid's silent and non-silent information. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!