微信公众号的粉丝管理

1. 首先在控制器下创建相应的方法,其中包括粉丝的显示,粉丝的同步。

<?php 
namespace Home\Controller;
use Think\Controller;
use LaneWeChat\Core\UserManage;
use LaneWeChat\Core\Curl;
class FriendController extends Controller 
{
	//显示粉丝
    public function index(){
    	$model = M('mp_friends');
    	$data = $model->distinct ('openid')->select();
    	$this->assign('data',$data);
       	$this->display();
    }
    //获取粉丝信息
    public function getfriend(){
    	$accessToken = getAccess_token();
    	$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$accessToken";
		include APP_PATH . 'LaneWeChat/lanewechat.php';
		$ret = Curl::callWebServer($url,'','GET');
		$total = $ret['total'];
		$count = $ret['count'];
		for ($i=0;$i<$count;$i++) {
			$openid = $ret['data']['openid'][$i];
			$data[] = UserManage::getUserInfo($openid);
		}
		foreach ($data as $k => $v) {
			$map['subscribe']=$v['subscribe'];
	        $map['openid']=$v['openid'];
	        $map['nickname']=$v['nickname'];
	        $map['sex']=$v['sex'];
	        $map['language']=$v['language'];
	        $map['city']=$v['city'];
	        $map['province']=$v['province'];
	        $map['country']=$v['country'];
	        $map['headimgurl']=$v['headimgurl'];
	        $map['subscribe_time']=$v['subscribe_time'];
	        $map['remark']=$v['remark'];
	        $map['groupid']=$v['groupid'];
	        $map['subscribe_scene']=$v['subscribe_scene'];
	        $map['qr_scene']=$v['qr_scene'];
	        $mp=getCurrentMp();
		    $map['mp_id']=$mp['id'];
	        $con['openid']=$map['openid'];
	        $data=M('mp_friends')->where($con)->find();
	        if($data){
	            $data=M('mp_friends')->select();
	            $this->assign('data',$data);
	            $this->display('index');
	            exit;
	        }else{
	            M('mp_friends')->add($map);
	        }
     	}
      $this->display('index');
	}
}
?>

UserManage中的getUserInfo方法如下:

<?php
/**
     * @descrpition 创建分组
     * @param $groupName 组名 UTF-8
     * @return JSON {"group": {"id": 107,"name": "test"}}
     */
    public static function createGroup($groupName){
        //获取ACCESS_TOKEN
        // $accessToken = AccessToken::getAccessToken();
        $accessToken = getAccess_token();
        $queryUrl = 'https://api.weixin.qq.com/cgi-bin/groups/create?access_token='.$accessToken;
        $data = '{"group":{"name":"'.$groupName.'"}}';
        return Curl::callWebServer($queryUrl, $data, 'POST');

    }
?>

2. 粉丝的数据需要存到数据库friends,数据库内容如下



3. 有了相应的方法和数据库,只需要将数据显示在相应的模板(html)即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值