在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号,同一用户的openid不同)。公众号可通过本接口来根据OpenID获取用户基本信息,包括昵称、头像、性别、所在城市、语言和关注时间。开发者可通过OpenID来获取用户基本信息。特别需要注意的是,如果开发者拥有多个移动应用、网站应用和公众帐号,可通过获取用户基本信息中的unionid来区分用户的唯一性,因为只要是同一个微信开放平台帐号下的移动应用、网站应用和公众帐号,用户的unionid是唯一的。换句话说,同一用户,对同一个微信开放平台下的不同应用,unionid是相同的。
获取用户基本信息,接口调用:https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
在前台页面用按钮调用addFuns方法,控制器中addFuns方法如下:
//同步粉丝 public function addFuns(){ $mp = $this->mp;//获取正在使用的公众号的ID include APP_PATH . 'LaneWeChat/lanewechat.php'; $access_token = getAccess_token(); $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token"; $ret = Curl::callWebServer($url,'','POST'); // print_r($ret); // exit; for ($i=0; $i < $ret['count']; $i++) { $openid = $ret['data']['openid'][$i]; $openids[$i]['openid'] = $openid; $openids[$i]['lang'] = 'zh_CN'; } // print_r($openids); // exit; $result = UserManage::getManyUserInfo($openids); // $result = $result['user_info_list']; if(isset($result['user_info_list'])){ $result = $result['user_info_list']; foreach ($result as &$value) { $value['mp_id'] = $mp['id']; $value['tagid_list'] = implode(",",$value['tagid_list']); } } $openid = M('mp_friends')->where("openid = '$openid'")->find(); if(!isset($openid)){ M('mp_friends')->addAll($result); } // $this->display('index'); $this->redirect('index'); }
获取用户基本信息之后,需要让信息显示在当前页面,首先查询数据库信息,然后让模板显示。代码如下:
public function index($tagid=''){ $mp = $this->mp; $where['mp_id'] = $mp['id']; if(!empty($tagid)){ $where['tagid_list'] = array('like',"%{$tagid}%"); } $data = M('mp_friends')->where($where)->field('id,headimgurl,nickname,subscribe_time,openid,tagid_list')->order()->select(); $tag = M('tags')->where($where)->select(); // print_r($tag); // exit; $this->assign('tag',$tag); $this->assign('data',$data); $this->display(); }
希望可以帮助到你们~~~嘿嘿