本篇文章给大家带来的内容是关于php如何获取微信用户基本信息(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
<?phperror_reporting(1);header('content-type:text/html; charset=utf-8');/* 配置开始 */$appid = ''; //微信公众平台->开发->基本配置->appid$appkey = ''; //微信公众平台->开发->基本配置->appsecret/* 配置结束 *///①、获取用户openid$wxpay = new wxservice($appid,$appkey);$data = $wxpay->getopenid(); //获取openidif(!$data['openid']) exit('获取openid失败');//②、获取用户信息$user = $wxpay->getuserinfo($data['openid'],$data['access_token']);?><!doctype html><html><head> <meta charset="utf-8"> <meta name="renderer" content="webkit" /> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <title>微信获取用户信息demo</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/jquery/2.1.0/jquery.min.js"></script></head><body><div> <div> <h1>你的基本信息如下:</h1> <table class="table table-bordered"> <tr> <td>openid</td> <td><?=$user['openid']?></td> </tr> <tr> <td>unionid</td> <td><?=$user['unionid']?></td> </tr> <tr> <td>昵称</td> <td><?=$user['nickname']?></td> </tr> <tr> <td>头像</td> <td><img src="<?=$user['headimgurl']?> style=width: 100px; alt=></td> </tr> <tr> <td>性别</td> <td><?php switch (strtoupper($user['sex'])){ case 1: echo '男性'; break; case 2: echo '女性'; break; default: echo '未知'; break; } ?></td> </tr> <tr> <td>省份 / 城市</td> <td><?=$user['province'].' / '.$user['city']?></td> </tr> <tr> <td>language</td> <td><?=$user['language']?></td> </tr> </table> </div></div></body></html><?phpclass wxservice{ protected $appid; protected $appkey; public $data = null; public function __construct($appid, $appkey) { $this->appid = $appid; //微信支付申请对应的公众号的appid $this->appkey = $appkey; //微信支付申请对应的公众号的app key } /** * 通过跳转获取用户的openid,跳转流程如下: * 1、设置自己需要调回的url及其其他参数,跳转到微信服务器https://open.weixin.qq.com/connect/oauth2/authorize * 2、微信服务处理完成之后会跳转回用户redirect_uri地址,此时会带上一些参数,如:code * * @return 用户的openid */ public function getopenid() { //通过code获得openid if (!isset($_get['code'])){ //触发微信返回code码 $baseurl = $this->getcurrenturl(); $url = $this->__createoauthurlforcode($baseurl); header(location: $url); exit(); } else { //获取code码,以获取openid $code = $_get['code']; $openid = $this->getopenidfrommp($code); return $openid; } } public function getcurrenturl() { $scheme = $_server['https']=='on' ? 'https://' : 'http://'; $uri = $_server['php_self'].$_server['query_string']; if($_server['request_uri']) $uri = $_server['request_uri']; $baseurl = urlencode($scheme.$_server['http_host'].$uri); return $baseurl; } /** * 通过code从工作平台获取openid机器access_token * @param string $code 微信跳转回来带上的code * @return openid */ public function getopenidfrommp($code) { $url = $this->__createoauthurlforopenid($code); $res = self::curlget($url); $data = json_decode($res,true); $this->data = $data; return $data; } /** * 构造获取open和access_toke的url地址 * @param string $code,微信跳转带回的code * @return 请求的url */ private function __createoauthurlforopenid($code) { $urlobj[appid] = $this->appid; $urlobj[secret] = $this->appkey; $urlobj[code] = $code; $urlobj[grant_type] = authorization_code; $bizstring = $this->tourlparams($urlobj); return https://api.weixin.qq.com/sns/oauth2/access_token?.$bizstring; } /** * 构造获取code的url连接 * @param string $redirecturl 微信服务器回跳的url,需要url编码 * @return 返回构造好的url */ private function __createoauthurlforcode($redirecturl) { $urlobj[appid] = $this->appid; $urlobj[redirect_uri] = $redirecturl; $urlobj[response_type] = code; $urlobj[scope] = snsapi_userinfo; $urlobj[state] = state; $bizstring = $this->tourlparams($urlobj); return https://open.weixin.qq.com/connect/oauth2/authorize?.$bizstring; } /** * 拼接签名字符串 * @param array $urlobj * @return 返回已经拼接好的字符串 */ private function tourlparams($urlobj) { $buff = ; foreach ($urlobj as $k => $v) { if($k != sign) $buff .= $k . = . $v . &; } $buff = trim($buff, &); return $buff; } /** * 获取用户信息 * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的openid * @return string */ public function getuserinfo($openid,$access_token) { $response = self::curlget('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_cn'); return json_decode($response,true); } public static function curlget($url = '', $options = array()) { $ch = curl_init($url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_timeout, 30); if (!empty($options)) { curl_setopt_array($ch, $options); } //https请求 不验证证书和host curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $data = curl_exec($ch); curl_close($ch); return $data; } public static function curlpost($url = '', $postdata = '', $options = array()) { if (is_array($postdata)) { $postdata = http_build_query($postdata); } $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_postfields, $postdata); curl_setopt($ch, curlopt_timeout, 30); //设置curl允许执行的最长秒数 if (!empty($options)) { curl_setopt_array($ch, $options); } //https请求 不验证证书和host curl_setopt($ch, curlopt_ssl_verifypeer, false); curl_setopt($ch, curlopt_ssl_verifyhost, false); $data = curl_exec($ch); curl_close($ch); return $data; }}
以上就是php如何获取微信用户基本信息(代码)的详细内容。