首頁  >  文章  >  後端開發  >  PHP微信開發同步粉絲

PHP微信開發同步粉絲

不言
不言原創
2018-04-27 14:53:132079瀏覽

這篇文章主要介紹了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[&#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;];
	}
}

呼叫封裝好的框架

#
 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;);
    }
//批量获取基本信息
    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;);
    }

做好準備工作後,開始同步

 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;));


     }

相關推薦:

PHP微信開發之範本訊息回覆

以上是PHP微信開發同步粉絲的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn