在做微信公众号的开发中,经常会用到页面跳转,但跳转后如何获取用户的openid呢?今天绵阳动力网站建设公司就为大家介绍关于利用微信跳转的Code参数获取用户的openid的实现方法:
关键代码如下所示:
//获取微信登录用户信息 function getOpenID($appid,$appsecret,$code){ $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=". $appsecret."&code=".$code."&grant_type=authorization_code"; $weixin=file_get_contents($url);//通过code换取网页授权access_token $jsondecode=json_decode($weixin); //对JSON格式的字符串进行编码 $array = get_object_vars($jsondecode);//转换成数组 $openid = $array['openid'];//输出openid return $openid; }
以上所述是绵阳动力网站建设公司给大家介绍的PHP通过微信跳转的Code参数获取用户的openid(关键代码),希望对大家有所帮助。