如何通过PHP获取微信用户的唯一标识openid?
- 内容介绍
- 文章标签
- 相关推荐
本文共计439个文字,预计阅读时间需要2分钟。
使用微信接口,无论是自动登录还是微信支付,我们首先需要获取的便是openid。获取openid的方式有两种,一种是在关注时获取,此时订阅号即可获取;另一种是通过网页授权。
使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的是认证服务号。
今天我要说的是第二种网页授权获取openid。下面是我写的一个关于获取openid的类
<?php /** * 微信授权相关接口 * * @link www.phpddt.com */ class Wchat { private $app_id = 'wx444444444444'; private $app_secret = '77777777'; private $state='aaaa'; /** * 获取微信授权链接 * * @param string $redirect_uri 跳转地址 * @param mixed $state 参数 */ public function get_authorize_url($redirect_uri = '', $state = '') { $redirect_uri = urlencode($redirect_uri); return "open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect"; } /** * 获取微信openid */ public function getOpenid($turl) { if (!isset($_GET['code'])){ //触发微信返回code码 $url=$this->get_authorize_url($turl, $this->state); Header("Location: $url"); exit(); } else { //获取code码,以获取openid $code = $_GET['code']; $access_info = $this->get_access_token($code); return $access_info; } } /** * 获取授权token网页授权 * * @param string $code 通过get_authorize_url获取到的code */ public function get_access_token($code = '') { $appid=$this->app_id; $appsecret=$this->app_secret; $token_url = "api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; //echo $token_url; $token_data = $this->'.$_SERVER['HTTP_HOST'].'/user.php?act=register'; $info=$wchat->getOpenid($t_url); if($info){ $openid=$info['openid']; setcookie('openid',$openid,time()+86400*30); } }
以上就是我总结的获取openid的方法啦。
以上就是php获取微信openid的详细内容,更多请关注易盾网络其它相关文章!
本文共计439个文字,预计阅读时间需要2分钟。
使用微信接口,无论是自动登录还是微信支付,我们首先需要获取的便是openid。获取openid的方式有两种,一种是在关注时获取,此时订阅号即可获取;另一种是通过网页授权。
使用微信接口,无论是自动登录还是微信支付我们首先需要获取的就是openid,获取openid的方式有两种,一种是在关注的时候进行获取,这种订阅号就可以获取的到,第二种是通过网页授权获取,这种获取需要的是认证服务号。
今天我要说的是第二种网页授权获取openid。下面是我写的一个关于获取openid的类
<?php /** * 微信授权相关接口 * * @link www.phpddt.com */ class Wchat { private $app_id = 'wx444444444444'; private $app_secret = '77777777'; private $state='aaaa'; /** * 获取微信授权链接 * * @param string $redirect_uri 跳转地址 * @param mixed $state 参数 */ public function get_authorize_url($redirect_uri = '', $state = '') { $redirect_uri = urlencode($redirect_uri); return "open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect"; } /** * 获取微信openid */ public function getOpenid($turl) { if (!isset($_GET['code'])){ //触发微信返回code码 $url=$this->get_authorize_url($turl, $this->state); Header("Location: $url"); exit(); } else { //获取code码,以获取openid $code = $_GET['code']; $access_info = $this->get_access_token($code); return $access_info; } } /** * 获取授权token网页授权 * * @param string $code 通过get_authorize_url获取到的code */ public function get_access_token($code = '') { $appid=$this->app_id; $appsecret=$this->app_secret; $token_url = "api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; //echo $token_url; $token_data = $this->'.$_SERVER['HTTP_HOST'].'/user.php?act=register'; $info=$wchat->getOpenid($t_url); if($info){ $openid=$info['openid']; setcookie('openid',$openid,time()+86400*30); } }
以上就是我总结的获取openid的方法啦。
以上就是php获取微信openid的详细内容,更多请关注易盾网络其它相关文章!

