微信公众平台模拟登录,获取账号信息

class wxOpenLogin{
	private $cookie_file ="";//cookie临时保存文件
	private $username = "";   //微信账号
	private $pwd = "";<span style="white-space:pre">	</span>//微信密码
	private $token = "";<span style="white-space:pre">	</span><span style="white-space:pre">	</span>
	private $useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"; 
	private $header = array( 
			'Accept:application/json, text/javascript, */*; q=0.01', 
			'Accept-Encoding:gzip,deflate,sdch', 
			'Accept-Language:zh-CN,zh;q=0.8', 
			'Connection:keep-alive', 
			'Host:mp.weixin.qq.com', 
			'Origin:https://mp.weixin.qq.com', 
			'Referer:https://mp.weixin.qq.com/', 
			); 
	private $postFields = array();
	public function __construct($u,$p){
		$this->username = $u;
		$this->pwd = $p;
		$this->cookie_file = tempnam('./tmp','cookie');
		$str_token = $this->doLogin();//接收包含有token的字符串
		$token = explode('&token=', $str_token);
		$this->token = str_replace('"}',"",$token[1]);
	}
	
	public function doLogin(){
		$url = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN"; 
		$PostData = array( 
			   "username" => $this->username, 
			   "pwd" => md5($this->pwd), 
			   "f" => "json",
			   "imgcode"=>''
		   );
		$ch = curl_init(); 
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_HTTPHEADER,$this->header); 
		curl_setopt($ch, CURLOPT_USERAGENT,$this->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_COOKIEJAR, $this->cookie_file); //添加cookie用JAR  
		curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
		$result = curl_exec($ch); 
		
		curl_close($ch);
		return $result;//登录成功,获取Token
	}
	
	public function getInfo(){	//获取公众账号的个人信息
		$url = 'https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&token='.$this->token.'&lang=zh_CN';
		
		$result = $this->post_curl($url,array(),$this->cookie_file);
		preg_match('|<div class="meta_content">.*?<img class="avatar" src="(.*?)" />.*?</div>.*?</li>|is',$result,$baseinfo1);//读取头像
		preg_match('|<li class="account_setting_item">.*?名称.*?<div class="meta_content">(.*?)</div>.*?</li>.*?<li class="account_setting_item">.*?原始ID.*?<div class="meta_content">.*?<span>(.*?)</span>.*?</div>.*?</li>.*?<li class="account_setting_item">.*?微信号.*?<div class="meta_content">.*?<span>(.*?)</span>.*?</div>.*?</li>|is',$result,$baseinfo);
		return  array_merge($baseinfo,$baseinfo1);
	}
	
	public function openDev(){//打开开发接口
		$this->postFields = array('flag'=>'1','type'=>'2','token'=>$token);
		$url = 'https://mp.weixin.qq.com/cgi-bin/skeyform?form=advancedswitchform&lang=zh_CN';
		$result = $this->post_curl($url,$this->postFields,$this->cookie_file);
	}
	
	private function post_curl($url,$postData,$cookie){
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);//关闭CA验证
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
		curl_setopt($ch, CURLOPT_HEADER,0); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		if(count($postData) > 0){
			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postData); //需要post数据时  加入此句
		}
		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
		$result = curl_exec($ch); 
		
		curl_close($ch);
		return $result;
		
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值