class wxOpenLogin{ private $cookie_file ="";//cookie临时保存文件 private $username = ""; //微信账号 private $pwd = ""; //微信密码 private $token = ""; 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('|.*?.*?.*?|is',$result,$baseinfo1);//读取头像 preg_match('|.*?名称.*?(.*?).*?.*?.*?原始ID.*?.*?(.*?).*?.*?.*?.*?微信号.*?.*?(.*?).*?.*?|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; } }