Home > Article > Backend Development > PHP WeChat development synchronization fans
This article mainly introduces the synchronization of fans in PHP WeChat development. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.
First get the id of the current public account
//获取正在使用的公众号 function getCurrentMp(){ $mp=M('mp')->where('is_use=1')->find(); return $mp; }
Initialization
private $mp; public function _initialize(){ $mp=getCurrentMp(); if(empty($mp)){ $this->error('无使用的公众号',U('mp/index')); exit; }else{ $this->mp=$mp; } }
Method to obtain access_token
function getAccess_token(){ $mp=M('mp')->where('is_use=1')->find(); if(empty($mp)) return false; $id=$mp['id'];//正在使用的公众号的主键 if(empty($mp['access_token']) || $mp['expire_time']<time()){ $appid=$mp['appid']; $appsecret=$mp['appsecret']; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; include APP_PATH.'LaneWeChat/lanewechat.php'; $arr= \LaneWeChat\Core\Curl::callWebServer($url,'','GET'); //将获取到的access_token存入数据库 if(isset($arr['access_token'])){ $data['access_token']=$arr['access_token']; $data['expire_time']=$arr['expires_in'] + time()-200; M('mp')->where("id=$id")->save($data); return $arr['access_token']; }else{ return false; } }else{ return $mp['access_token']; } }
Calling the encapsulated framework
public static function getFansList($next_openid=''){ //获取ACCESS_TOKEN $accessToken = getAccess_token(); if(empty($next_openid)){ $queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$accessToken; }else{ $queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$accessToken.'&next_openid='.$next_openid; } return Curl::callWebServer($queryUrl, '', 'GET'); }
//批量获取基本信息 public function getManyUserInfo($openids){ $accessToken = getAccess_token(); $queryUrl = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token='.$accessToken; $json=json_encode(array('user_list'=>$openids)); return Curl::callWebServer($queryUrl, $json, 'POST'); }
After making preparations, start synchronization
public function downFans(){ $mp=$this->mp; $mp_id = $mp['id']; include APP_PATH.'LaneWeChat/lanewechat.php'; $ret=UserManage::getFansList(); // print_r($ret); $openids=$ret['data']['openid']; // print_r($openids); $arr=array(); foreach ($openids as $value) { $row=array(); $row['openid']=$value; $row['lang']="zh_CN"; $arr[]=$row; } // print_r($arr); // exit; $ret=UserManage::getManyUserInfo($arr); // print_r($ret); // exit; if(isset($ret['user_info_list'])){ $data=$ret['user_info_list']; // print_r($data); // exit; $mp=$this->mp; // $data['mp_id']= $mp_id; $fan=M('mp_friends'); $fan->where("mp_id={$mp['id']}")->delete(); foreach ($data as &$value) { $value['mp_id']=$mp['id']; $value['tagid_list']=implode(',', $value['tagid_list']); } $fan->addAll($data); } $this->success('同步完成',U('index')); }
Related recommendations:
Template message reply for PHP WeChat development
The above is the detailed content of PHP WeChat development synchronization fans. For more information, please follow other related articles on the PHP Chinese website!