微信如何区分静默和非静默获取openid的方法?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1212个文字,预计阅读时间需要5分钟。
不知道大家有没有使用过微信的openid,但今天小编要带大家了解一下如何使用PHP获取微信openid。以下是获取微信openid的静态默认和非默认方法的简要说明,需要的伙伴可以参考参考。
静态默认方法:
1.首先需要配置微信公众号的设置,获取AppID和AppSecret。
2.使用以下PHP代码获取静态默认openid:
$url=https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code;$result=json_decode(file_get_contents($url), true);$openid=$result['openid'];
echo $openid;?>
非默认方法:
1.同样需要配置微信公众号的设置,获取AppID和AppSecret。
2.使用以下PHP代码获取非默认openid:
php$url=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$appid&secret=$appsecret&code=$code&grant_type=refresh_token;$result=json_decode(file_get_contents($url), true);$openid=$result['openid'];
echo $openid;?>
以上是获取微信openid的静态默认和非默认方法,希望对大家有所帮助。
不知道你们有没有用过微信的openid,但是今天小编要带大家一起了解一个如何使用PHP,获取到微信openid的静默及非静默,有需要的小伙伴可以参考参考。<?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 = '' . $_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 = "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 = "api.weixin.qq.com/sns/oauth2/access_token?" . $paramsUrl; $data = $this->api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN");即可获取打印用户信息,或者用这里的curl方法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; } }
本文共计1212个文字,预计阅读时间需要5分钟。
不知道大家有没有使用过微信的openid,但今天小编要带大家了解一下如何使用PHP获取微信openid。以下是获取微信openid的静态默认和非默认方法的简要说明,需要的伙伴可以参考参考。
静态默认方法:
1.首先需要配置微信公众号的设置,获取AppID和AppSecret。
2.使用以下PHP代码获取静态默认openid:
$url=https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code;$result=json_decode(file_get_contents($url), true);$openid=$result['openid'];
echo $openid;?>
非默认方法:
1.同样需要配置微信公众号的设置,获取AppID和AppSecret。
2.使用以下PHP代码获取非默认openid:
php$url=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=$appid&secret=$appsecret&code=$code&grant_type=refresh_token;$result=json_decode(file_get_contents($url), true);$openid=$result['openid'];
echo $openid;?>
以上是获取微信openid的静态默认和非默认方法,希望对大家有所帮助。
不知道你们有没有用过微信的openid,但是今天小编要带大家一起了解一个如何使用PHP,获取到微信openid的静默及非静默,有需要的小伙伴可以参考参考。<?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 = '' . $_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 = "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 = "api.weixin.qq.com/sns/oauth2/access_token?" . $paramsUrl; $data = $this->api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN");即可获取打印用户信息,或者用这里的curl方法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; } }

