Home > Backend Development > PHP Tutorial > PHP WeChat development synchronization fans

PHP WeChat development synchronization fans

不言
Release: 2023-03-24 22:48:01
Original
2194 people have browsed it

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;
}
Copy after login

Initialization

private $mp;
	public function _initialize(){
     	$mp=getCurrentMp();
		if(empty($mp)){
			$this->error('无使用的公众号',U('mp/index'));
			exit;
		}else{
			$this->mp=$mp;
		}
     }
Copy after login


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[&#39;access_token&#39;]) || $mp[&#39;expire_time&#39;]<time()){
		$appid=$mp[&#39;appid&#39;];
		$appsecret=$mp[&#39;appsecret&#39;];
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;

		include APP_PATH.&#39;LaneWeChat/lanewechat.php&#39;;
		$arr= \LaneWeChat\Core\Curl::callWebServer($url,&#39;&#39;,&#39;GET&#39;);
		//将获取到的access_token存入数据库
		if(isset($arr[&#39;access_token&#39;])){
			$data[&#39;access_token&#39;]=$arr[&#39;access_token&#39;];
			$data[&#39;expire_time&#39;]=$arr[&#39;expires_in&#39;] + time()-200;

			M(&#39;mp&#39;)->where("id=$id")->save($data);
			return $arr[&#39;access_token&#39;];
		}else{
			return false;
		}
	}else{
		return $mp[&#39;access_token&#39;];
	}
}
Copy after login

Calling the encapsulated framework

 public static function getFansList($next_openid=&#39;&#39;){
        //获取ACCESS_TOKEN
        $accessToken = getAccess_token();
        if(empty($next_openid)){
            $queryUrl = &#39;https://api.weixin.qq.com/cgi-bin/user/get?access_token=&#39;.$accessToken;
        }else{
            $queryUrl = &#39;https://api.weixin.qq.com/cgi-bin/user/get?access_token=&#39;.$accessToken.&#39;&next_openid=&#39;.$next_openid;
        }
        return Curl::callWebServer($queryUrl, &#39;&#39;, &#39;GET&#39;);
    }
Copy after login
//批量获取基本信息
    public function getManyUserInfo($openids){
         $accessToken = getAccess_token();
         $queryUrl = &#39;https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=&#39;.$accessToken;
         $json=json_encode(array(&#39;user_list&#39;=>$openids));
        return Curl::callWebServer($queryUrl, $json, &#39;POST&#39;);
    }
Copy after login

After making preparations, start synchronization

 public function downFans(){
     	$mp=$this->mp;
          $mp_id = $mp[&#39;id&#39;];
     	
     	include APP_PATH.&#39;LaneWeChat/lanewechat.php&#39;;
     	$ret=UserManage::getFansList();
     	// print_r($ret);
     	$openids=$ret[&#39;data&#39;][&#39;openid&#39;];
     	// print_r($openids);
     	$arr=array();
     	foreach ($openids as $value) {
     		$row=array();
     		$row[&#39;openid&#39;]=$value;
     		$row[&#39;lang&#39;]="zh_CN";
     		$arr[]=$row;
     	}
     	// print_r($arr);
     	// exit;
     	$ret=UserManage::getManyUserInfo($arr);
     	// print_r($ret);
     	// exit;
     	if(isset($ret[&#39;user_info_list&#39;])){
     		$data=$ret[&#39;user_info_list&#39;];
     		// print_r($data);
     	 //    exit;
               $mp=$this->mp;
               // $data[&#39;mp_id&#39;]= $mp_id;
           	$fan=M(&#39;mp_friends&#39;);
     	     $fan->where("mp_id={$mp[&#39;id&#39;]}")->delete();
     	     foreach ($data as &$value) {
          	    	
                    $value[&#39;mp_id&#39;]=$mp[&#39;id&#39;];
                    $value[&#39;tagid_list&#39;]=implode(&#39;,&#39;, $value[&#39;tagid_list&#39;]);
                    
     	     }
               $fan->addAll($data);
               
           	
     	}
         $this->success(&#39;同步完成&#39;,U(&#39;index&#39;));


     }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template