这篇文章主要介绍了PHP微信开发之同步粉丝 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
首先获取到当前公众号的id
//获取正在使用的公众号 function getCurrentMp(){ $mp=M('mp')->where('is_use=1')->find(); return $mp; }
初始化
private $mp; public function _initialize(){ $mp=getCurrentMp(); if(empty($mp)){ $this->error('无使用的公众号',U('mp/index')); exit; }else{ $this->mp=$mp; } }
获取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']; } }
调用封装好的框架
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'); }
做好准备工作后,开始同步
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')); }
相关推荐:
以上是PHP微信开发之同步粉丝 的详细内容。更多信息请关注PHP中文网其他相关文章!

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

到Improveyourphpwebsite的实力,UsEthestertate:1)emplastOpCodeCachingWithOpcachetCachetOspeedUpScriptInterpretation.2)优化的atabasequesquesquesquelies berselectingOnlynlynnellynnessaryfields.3)usecachingsystemssslikeremememememcachedisemcachedtoredtoredtoredsatabaseloadch.4)

是的,itispossibletosendMassemailswithp.1)uselibrarieslikeLikePhpMailerorSwiftMailerForeffitedEmailSending.2)enasledeLaysBetemailStoavoidSpamflagssspamflags.3)sylectynamicContentToimpovereveragement.4)

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

使用PHP发送电子邮件的最佳方法包括:1.使用PHP的mail()函数进行基本发送;2.使用PHPMailer库发送更复杂的HTML邮件;3.使用SendGrid等事务性邮件服务提高可靠性和分析能力。通过这些方法,可以确保邮件不仅到达收件箱,还能吸引收件人。

计算PHP多维数组的元素总数可以使用递归或迭代方法。1.递归方法通过遍历数组并递归处理嵌套数组来计数。2.迭代方法使用栈来模拟递归,避免深度问题。3.array_walk_recursive函数也能实现,但需手动计数。

在PHP中,do-while循环的特点是保证循环体至少执行一次,然后再根据条件决定是否继续循环。1)它在条件检查之前执行循环体,适合需要确保操作至少执行一次的场景,如用户输入验证和菜单系统。2)然而,do-while循环的语法可能导致新手困惑,且可能增加不必要的性能开销。

在PHP中高效地哈希字符串可以使用以下方法:1.使用md5函数进行快速哈希,但不适合密码存储。2.使用sha256函数提高安全性。3.使用password_hash函数处理密码,提供最高安全性和便捷性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3汉化版
中文版,非常好用