微度云课堂

我们登陆微信后台然后同步用户的信息就是根据这个方法同步的,就待必须用fackid这个东西,但是呢有个不幸的消息要告诉大家那就是腾讯内部禁止一个东西,就是如果用户24小时内没有给你发消息,你不能主动给用户发送消息,这个其实就是再推广他的高级功能,收收认证费了!下面的视频是讲解获取fackid和用户名的,大家可以看视频,然后代码在最下边
Wechat.class.php 微信fackid的demo代码
class wechat{

 	private $username;
 	private $password;
 	private $cookie;
 	private $token;
 	private $referer;
 	private $userfack;
 	
 	public function __construct($username,$password){
 		$this->username = $username;
 		$this->password = $password;
 		$this->dologin();
 		$this->userfack = $this->getfackid();
 		var_dump($this->userfack);
 	}

 	private function dologin(){
 		$header = array(
 			'Accept:application/json, text/javascript, */*; q=0.01',
			'Accept-Encoding:gzip,deflate,sdch',
			'Accept-Language:zh-CN,zh;q=0.8,ja;q=0.6,en;q=0.4',
			'AlexaToolbar-ALX_NS_PH:AlexaToolbar/alxg-3.2',
			'Connection:keep-alive',
			'Host:mp.weixin.qq.com',
			'Origin:https://mp.weixin.qq.com',
			'Referer:https://mp.weixin.qq.com/',
 		);
 		$PostData = array(
 				"username"=>$this->username,
 				"pwd" =>md5($this->password),
 				"f"=>"json"
 		);
 		$useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
 		$url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
 		$ch = curl_init();
 		curl_setopt($ch, CURLOPT_URL, $url);
 		curl_setopt($ch, CURLOPT_HTTPHEADER,$header); 
 		curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_POST, 1); 
		curl_setopt ($ch, CURLOPT_POSTFIELDS, $PostData);
		curl_setopt($ch, CURLOPT_HEADER, 1); 
		curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		$result = curl_exec($ch);
		curl_close($ch);
		$data = explode("\n",$result);
		foreach ($data as $key => $value) {
			if(preg_match("/ErrMsg/i", $value)){
				$this->token = rtrim(substr($value,strrpos($value,"=")+1,-1),'",');
			}
			if(preg_match("/^set-cookie:[\s]+([^=]+)=([^;]+)/i", $value,$match)){
				$this->cookie .= $match[1].'='.$match[2].'; ';
			}
		}
 	}

 	//获取fackid
 	public function getfackid(){
 		$pageSize = 10000;
 		$url = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={$pageSize}&pageidx=0&type=0&groupid=0&token={$this->token}&lang=zh_CN";
 		$this->referer = "https://mp.weixin.qq.com/cgi-bin/home?t=home/index&lang=zh_CN&token={$_SESSION['token']}";
 		$data = $this->wget($url);
 		preg_match_all("/\"id\":(\d+),\"name\"/",$data,$result);
 		$groupid = $result[1];
 		$num = 0;
 		foreach ($groupid as $value) {
 			$url = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize={$pageSize}&pageidx=0&type=0&groupid={$value}&token={$this->token}&lang=zh_CN";
 			$userurl = $this->wget($url);
 			preg_match_all("/\"id\":(\d+),\"nick_name\":\"([^\"]+)\"/", $userurl,$reuser);
 			foreach ($reuser[1] as $key => $value) {
 				$userdata[$num]["fackid"]=$value;
 				$userdata[$num]["name"] = $reuser[2][$key];
 				$num++;
 			}
 		}
 		return $userdata;
 	}

 	private function wget($url){
 		$header = array(
 			'Accept:*/*',
			'Accept-Encoding:gzip,deflate',
			'Accept-Language:zh-CN,zh;q=0.8',
			'Connection:keep-alive',
			'Host:mp.weixin.qq.com',
			'Referer:'.$this->referer,
			'X-Requested-With:XMLHttpRequest'
 		);
 		$useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
 		$ch = curl_init();
 		curl_setopt($ch, CURLOPT_URL, $url);
 		curl_setopt($ch, CURLOPT_HTTPHEADER,$header); 
 		curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
 		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
		curl_setopt($ch, CURLOPT_HTTPGET, 1);
		curl_setopt($ch, CURLOPT_AUTOREFERER,1);
		curl_setopt($ch, CURLOPT_HEADER, 0); 
		curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		$result = curl_exec($ch);
		if (curl_errno($curl)) {
			echo 'Errno'.curl_error($curl);
		}
		curl_close($ch);
		return $result;
 	}
 }