通过php获取用户微信openid
// 基于CI框架
// 访问开始页面
public function url()
{
// wxAction/oauth2 微信回调地址;微信传入code值,通过该code在wxAction/oauth2请求当前用户微信资料
// account/bind 用户账号与微信号进行绑定
$current_url = site_url('wxAction/oauth2').'?returl=account/bind';
echo 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.W_APPID.'&redirect_uri='.urlencode($current_url).'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
}
// 微信回调地址,请求当前微信用户资料
public function oauth2()
{
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".W_APPID."&secret=".W_APPSECRET."&code=".$_GET['code']."&grant_type=authorization_code";
$content = file_get_contents($url);
$ret = json_decode($content, true);
if (isset($ret['openid'])) {
$this->session->set_userdata('OPENID', $ret['openid']);
//跳转回之前的页面
if ($return_url = $this->input->get('returl')) {
redirect($return_url);
} else {
redirect('welcome');
}
exit;
} else {
echo '网络请求繁忙,获取用户信息失败,请稍后再试!';
exit;
}
}
-----------------------------------------------------
说明:
a).代码仅供学习交流
b).本文根据自身经验及网络总结所作,如有错误,谢谢指教
c).转载请注明出处。
-----------------------------------------------------
说明:
a).代码仅供学习交流
b).本文根据自身经验及网络总结所作,如有错误,谢谢指教
c).转载请注明出处。
-----------------------------------------------------
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步