PHP微信公共号授权,获取openid、unionid。


/**
* 获取code
* @return code code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期
*/
public function actionIndex(){
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=".$this->secret."&code=".$_GET['code']."&grant_type=authorization_code";
$token = file_get_contents($url);
$token = json_decode($token,true);
$token["access_token"];
$user_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$token['access_token']."&openid=".$token["openid"]."&lang=zh_CN";
$userinfo = file_get_contents($user_url);
$arr = json_decode($userinfo,true);
$openid = $arr['openid'];

$unionid = $arr['unionid'];/*该字段必须在微信开发平台(https://open.weixin.qq.com/)进行关联*/

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM