<?php
header("content-type:text/html;charset=utf-8");/*** wx_mass* * 完成微信公众账户模拟登陆后的一系列操作* mass($content) 调用微信群发接口,群发文本信息* getUserList($page,$pagesize,$group) 获取用户信息* * 群发demo* * $user=array('account'=>'公众账户名称','password'=>'密码');* $obj=new wx_mass($user);* $obj->mass('感谢您关注优乐购物,这条消息是由测试程序发出,给你带来不便,敬请谅解!');* * @package wxdl* @author 我好笨* @copyright 2014* @version $Id$* @access public*/
class wx_mass
{private $_cookiefile;private $_cookieexpired = 3600;private $_account;private $_password;private $_token;public function __construct($options){$this->_account = isset($options['account'])?$options['account']:'';$this->_password = isset($options['password'])?$options['password']:'';//必须使用绝对路径$this->_cookiefile=dirname(__FILE__).'/temp/'.md5($this->_account);//此处需要创建一个文件夹装cookie文件$this->getCookie();}/*** wx_mass::mass()* * 微信群发功能* * @param mixed $content 群发内容* @return 成功返回ture,失败返回fasle*/public function mass($content){$refer="https://mp.weixin.qq.com/cgi-bin/masssendpage?t=mass/send&token=".$this->_token."&lang=zh_CN";$submit="https://mp.weixin.qq.com/cgi-bin/masssend";$post['ajax']=1;$post['city']='';$post['content']=$content;$post['country']='';$post['f']='json';$post['groupid']=-1;$post['imgcode']='';$post['lang']='zh_CN';$post['province']='';$post['random']=$this->randomFloat(0,1);$post['sex']=0;$post['synctxnews']=0;$post['synctxweibo']=0;$post['t']='ajax-response';$post['token']=$this->_token;$post['type']=1;$tmpInfo=$this->curl($submit,$refer,false,true,$post);$result = json_decode($tmpInfo,true);if (!isset($result['base_resp']) || $result['base_resp']['ret'] != 0){return false;}return true;}/*** wx_mass::getUserList()* * 根据分组ID获取用户列表* * @param integer $page* @param integer $pagesize* @param integer $groupid* @return*/public function getUserList($page=0,$pagesize=10,$groupid=100){$t = time().strval(mt_rand(100,999));$referer = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=0&lang=zh_CN&token=".$this->_token;$submit = "https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=$groupid&lang=zh_CN&f=json&token=".$this->_token;$result=$this->curl($submit,$referer);$json = json_decode($result,true);if(isset($json['contact_list'])){$json = json_decode($json['contact_list'],true);if(isset($json['contacts'])){return $json['contacts'];}}return false;}/*** wx_mass::login()* * 模拟登陆* * @return void*/private function login(){$submit = "https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";$refer = "https://mp.weixin.qq.com/";$post["username"] = $this->_account;$post["pwd"] = md5($this->_password);$post["f"] = "json";$post["imgcode"] = "";$tmpInfo=$this->curl($submit,$refer,true,true,$post);$result = json_decode($tmpInfo,true);if (!isset($result['base_resp']) || $result['base_resp']['ret'] != 0){return false;}preg_match("/token=(\d+)/i",$result['redirect_url'],$matches);if($matches){$this->_token = $matches[1];}}/*** wx_mass::curl()* * @param mixed $submit curl访问目标URL* @param mixed $refer 伪造的refer* @param bool $write_cookie 读写cookie,true为保存,false为读取* @param bool $is_post 是否post提交数据,true为提交数据,提交数据时,$post为提交的值* @param mixed $post is_post为真时,提交的数据* @return 返回curl执行结果*/private function curl($submit,$refer,$write_cookie=false,$is_post=false,$post=array()){$ch = curl_init(); // 启动一个CURL会话curl_setopt($ch, CURLOPT_URL, $submit); // 要访问的地址curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在curl_setopt($ch, CURLOPT_SSLVERSION, 3);curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器curl_setopt($ch, CURLOPT_REFERER, $refer);if($is_post){curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Post提交的数据包}curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环if($write_cookie && file_exists($this->_cookiefile)){@unlink($this->_cookiefile);}if($write_cookie){curl_setopt($ch,CURLOPT_COOKIEJAR,$this->_cookiefile);}else{curl_setopt($ch, CURLOPT_COOKIEFILE, $this->_cookiefile);}curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$tmpInfo = curl_exec($ch); // 执行操作var_dump($tmpInfo);if(curl_errno($ch)){$tmpInfo='Errno'.curl_error($ch);//捕抓异常}curl_close($ch); // 关闭CURL会话return $tmpInfo;}/*** wx_mass::getCookie()* * 获取cookie,检测cookie有效性* * @return void*/private function getCookie(){//登录获取cookie及token$this->login();}private function randomFloat($min = 0, $max = 1){return $min + mt_rand() / mt_getrandmax() * ($max - $min);}
}
$arr = array("account"=>"公众账户名称","password"=>"密码");
$aar = new wx_mass($arr);$abc = $aar -> getUserList();var_dump($abc);
?>