Home > Article > Backend Development > How to use WeChat to get openid’s silent and non-silent information
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.
<?php /* 需要的微信公众号配置信息 APPID : 绑定支付的APPID APPSECRET : 公众帐号secert */ class Index { // 配置账号信息 private $wxPayConfig = array (); protected function _initialize() { // 如果有继承的父类,则需先访问父类构造方法 // parent::_initialize (); // APPID:绑定支付的APPID(必须配置,开户邮件中可查看) $this->wxPayConfig ['APPID'] = 'wx426b3015555a46be'; // APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置) $this->wxPayConfig ['APPSECRET'] = '7813490da6f1265e4901ffb80afaa36f'; /** * 这里设置代理机器,只有需要代理的时候才设置,不需要代理,则设置为0.0.0.0和0 * 本例程通过curl使用HTTP POST方法,此处可修改代理服务器, * 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置) */ $this->wxPayConfig ['CURL_PROXY_HOST'] = "0.0.0.0"; // "10.152.18.220"; $this->wxPayConfig ['CURL_PROXY_PORT'] = 0; } // 通过code获得openid public function getOpenid() { //snsapi_base (未关注,不弹出授权页面,直接跳转,只能获取用户openid) //snsapi_userinfo (弹出授权页面,即使在未关注的情况下,只要用户授权,也能获取其信息,可通过openid拿到昵称、性别、所在地等) $scope = "snsapi_base";//这里使用静默授权 // 判断如果没有传 code 则先获取 code if (! isset ( $_GET ['code'] )) { // 获取当前打开页面的完整访问路径 $baseUrl = 'http://' . $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'] . $_SERVER ['QUERY_STRING']; $baseUrl = urlencode ( $baseUrl ); //配置获取code 路径的相关参数 $paramsArr = array(); $paramsArr ["appid"] = $this->wxPayConfig ['APPID']; $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 ['code']; //配置获取 openid 路径的相关参数 $paramsArr = array(); $paramsArr ["appid"] = $this->wxPayConfig ['APPID']; $paramsArr ["secret"] = $this->wxPayConfig ['APPSECRET']; $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 ['openid']; if($scope == 'snsapi_userinfo'){ //如果是显示授权,即需要用户点击授权,则还会返回 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['access_token']; } 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 ['CURL_PROXY_HOST'] != "0.0.0.0" && $this->wxPayConfig ['CURL_PROXY_PORT'] != 0) { curl_setopt ( $ch, CURLOPT_PROXY, $this->wxPayConfig ['CURL_PROXY_HOST'] ); curl_setopt ( $ch, CURLOPT_PROXYPORT, $this->wxPayConfig ['CURL_PROXY_PORT'] ); } // 运行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; } }
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!