使用curl实现模拟登录由来已久,但应用的微信中也是最近的事。在微盟等系统中可以不进入微信公众平台即可实现微信公众平台的一些功能,想来就是使用curl实现,最近正好用到这么个东西,所以便实现了一下。
1、首先介绍一下php 中的curl:
PHP支持的由Daniel Stenberg创建的libcurl库允许你与各种的服务器使用各种类型的协议进行连接和通讯。libcurl目前支持http、http、ftp、gopher、telnet、dict、file和ldap协议。libcurl同时也支持HTTPS认证、HTTP POST、HTTP PUT、 FTP 上传(这个也能通过PHP的FTP扩展完成)、HTTP 基于表单的上传、代理、cookies和用户名+密码的认证。
直白的说,php的curl可以模拟访问浏览器的访问从而实现对服务器的数据请求,从而在不载入具体页面的情况下与服务器端进行交互以及获取所需的数据
具体的介绍和API请见:curl手册
2、具体实现
将微信公众平台的模拟登录、信息获取和接口绑定功能都封装在类wechat中
(1)__construct($user_name, $password); 构造函数,创建wechat类,传入对应的账户名和密码
(2)dologin(); 模拟登录微信公众平台,后续的操作必须在模拟登录成功获取cookie和token后才可以顺利进行
(3)changeUrl();将微信龚中平平台绑定到特定的 url 和token
(4)getUserInfo();获取当前账户的开发者信息
(5)使用类似的手段可以模拟微信公众平台的各种操作
注:因为该代码在项目环境下使用,所以有些特殊之处专门为项目设计,故仅作参考,谢谢。
class wechat{ private $username; private $password; private $cookie; private $token; private $cookie_file; private $cookie_file_bk; public $user_info; public function __construct($username,$password){ $this->username = $username; $this->password = $password; $this->cookie_file_bk = dirname(__FILE__).'/temp/cookie2.txt'; $this->cookie_file = dirname(__FILE__).'/temp/cookie.txt'; } public function getToken(){ return $this->token; } public function getUserInfo(){ $getUrl ="http://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/index&action=index&token=".$this->token."&lang=zh_CN"; $getHeader = array( "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding:gzip,deflate", "Accept-Language:zh-CN,zh;q=0.8,en;q=0.6", "Connection:keep-alive" , "Host:mp.weixin.qq.com", "Referer:http://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token={$this->token}&lang=zh_CN", "User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36", ); $useragent = "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36"; $ch3 = curl_init(); curl_setopt($ch3, CURLOPT_ENCODING, ""); curl_setopt($ch3, CURLOPT_URL, $getUrl); curl_setopt($ch3, CURLOPT_REFERER,'http://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token={$this->token}&lang=zh_CN'); curl_setopt($ch3, CURLOPT_HTTPHEADER,$getHeader); curl_setopt($ch3, CURLOPT_USERAGENT,$useragent); curl_setopt($ch3, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch3, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE); //curl_setopt($ch3, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch3, CURLOPT_HEADER, 1); curl_setopt($ch3, CURLOPT_COOKIEFILE, $this->cookie_file); curl_setopt($ch3, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch3); $start = strpos($response,"Content-Length:"); $htmlSource = substr($response,($start+21)); //$htmlSource = mb_convert_encoding($htmlSource,"utf-8","gb2312"); $html = new simple_html_dom(); $html->load($htmlSource); $set_area = $html->find('div[class=meta_content]'); $paras = array(); $paras['name'] = trim($set_area[0]->innertext); $paras['email'] = trim($set_area[2]->innertext); $paras['origin_id'] = trim($set_area[3]->plaintext); $paras['wechat'] = trim($set_area[4]->plaintext); $paras['type'] = trim($set_area[5]->innertext); $paras['city'] = trim($set_area[9]->innertext); $this->user_info = $paras; var_dump($paras); } public function changeUrl($id){ $postUrl = "http://mp.weixin.qq.com/advanced/callbackprofile?t=ajax-response&token=" . $this->token . "&lang=zh_CN"; $postHeader = array( 'Accept:application/json, text/javascript, */*; q=0.01', 'Accept-Language:zh-CN,zh;q=0.8,en;q=0.6', 'Connection:keep-alive', 'Host:mp.weixin.qq.com', 'Origin:http://mp.weixin.qq.com', 'Pragma:no-cache', 'Referer:http://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/interface&token={$this->token}&lang=zh_CN', ); $server_url = "http://xxx.xxxx.com/wxplat/wx.php?id=mt_" . $id;//绑定的url $PostData = array( "url"=>$server_url, "callback_token"=>"bond_token"//绑定的token ); //$postData = "url=http://songweichat.sinaapp.com/wx_sample.php&callback_token=weixin"; $useragent = "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; $ch2 = curl_init(); curl_setopt($ch2, CURLOPT_URL, $postUrl); curl_setopt($ch2, CURLOPT_REFERER,'Referer:http://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/interface&token={$this->token}&lang=zh_CN'); curl_setopt($ch2, CURLOPT_HTTPHEADER,$postHeader); curl_setopt($ch2, CURLOPT_USERAGENT,$useragent); curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch2, CURLOPT_POST, 1); curl_setopt($ch2, CURLOPT_POSTFIELDS, $PostData); curl_setopt($ch2, CURLOPT_HEADER, 1); curl_setopt($ch2, CURLOPT_COOKIEFILE, $this->cookie_file); curl_setopt($ch2, CURLOPT_COOKIEJAR, $this->cookie_file_bk); curl_setopt($ch2, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch2); if (curl_errno($ch2)){ echo 'Errno'.curl_error($ch); } var_dump($response); } public 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:http://mp.weixin.qq.com', 'Referer:http://mp.weixin.qq.com/', ); $PostData = array( "username"=>$this->username, "pwd" =>md5($this->password), "f"=>"json" ); $useragent = "Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; $url = "http://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); if(file_exists($this->cookie_file)){ unlink($this->cookie_file); } curl_setopt($ch,CURLOPT_COOKIEJAR,$this->cookie_file); 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("/Err_Msg/i", $value)){ $ret = substr($value,strpos($value,"err_msg")+10,2); if($ret!="ok"){ echo $ret; return false; }else{ $this->token = substr($value,strrpos($value,"=")+1,-2); return true; } } } } } ?>