<?php echo '<meta http-equiv="content-type" content="text/html;charset=utf-8">';//防止乱码 error_reporting( E_ALL ^ E_NOTICE ); // 使用说明: // 开始要登录 $param = array(); $param['username'] = '你的公共微信账号'; $param['pwd'] = '你的密码'; echo '<pre>'; $wx = new Weixin(); $flag = $wx->login($param); echo "登录:\n"; var_dump($flag); /** echo "\n"; echo "获取分组:\n"; $group = $wx->getGroup(); var_dump($group); echo "\n"; echo "分组成员:\n"; $group = $wx->getFriendByGroup('0'); var_dump($group); **/ //代码只适应于文字信息,图文没有测试,图文的只是链接里的参数不一样,判断一下就OK了 echo "\n"; $msg = $wx->newmesg(0); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." ";//如果是nickname打印出微信名称 } if($key1=="date_time") { echo date("Y-m-d",$v1)." ";//如果是时间字段,格式打印出时间 } if($key1=="content") { echo "$v1"."<br>";//打印发送过来的内容 } } } echo "\n"; $msg = $wx->newmesg(20); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(40); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(60); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(80); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(100); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(120); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(140); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(160); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(180); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(200); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(220); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(240); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(260); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(260); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(280); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(300); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(3200); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(340); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(360); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(380); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(400); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } echo "\n"; $msg = $wx->newmesg(420); foreach($msg as $key=>$v){ foreach($v as $key1=>$v1){ // echo "$key1:$v1"."<br>"; if($key1=="nick_name") { echo "$v1"." "; } if($key1=="date_time") { echo date("Y-m-d",$v1)." "; } if($key1=="content") { echo "$v1"."<br>"; } } } /** echo "\n"; echo "获取图文:\n"; $mesg = $wx->getMsg(); var_dump($mesg); echo "\n"; echo "发送消息:\n"; **/ // 说明:如果$content为文字发送文本消息 // 说明:如果$content为图文ID则发送图文消息 //$content = '测试文本'; // 文本 //$content = '10000023'; // 图文 素材id //$mesg = $wx->battchMesgByGroup('101', $content); //var_dump($mesg); /**$arr = array( 'fakeId'=>'985865180', "nickName"=>"逄锦涛", "remarkName"=>'', 'content'=>'10000002' ); $s=$wx->sendmesg($arr); var_dump($s); echo "df"; **/ /** * 微信公众平台操作 * 基本于PHP-CURL * * @author phpbin * */ class Weixin { /** * PHP curl头部分 * * @var array */ private $_header; /** * 通讯cookie * * @var string */ private $_cookie; /** * 令牌 * * @var string */ private $_token; /** * 初始化,设置header */ public function __construct() { $this->_header = array(); $this->_header[] = "Host:mp.weixin.qq.com"; $this->_header[] = "Referer:https://mp.weixin.qq.com/cgi-bin/getmessage"; } /** * 用户登录 * 结构 $param = array('username'=>'', 'pwd'=>''); * * @param array $param * @return boolean */ public function login($param) { $url = 'https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN'; $post = 'username='.urlencode($param['username']).'&pwd='.md5($param['pwd']).'&imgcode=&f=json'; $stream = $this->_html($url, $post); // 判断是不是登录成功 $html = preg_replace("/^.*\{/is", "{", $stream); $json = json_decode($html, true); //获取 token preg_match("/lang=zh_CN&token=(\d+)/is", $json['ErrMsg'], $match); $this->_token = $match[1]; // 获取cookie $this->_cookie($stream); return (boolean)$this->_token; } /** * 获取图文消息 * * @return array */ public function getMsg() { $url = 'https://mp.weixin.qq.com/cgi-bin/operate_appmsg?token='.$this->_token.'&lang=zh_CN&sub=list&type=10&subtype=3&t=wxm-appmsgs-list-new&pagesize=10&pageidx=0&lang=zh_CN'; $stream = $this->_html($url); // 分析分组中好友 preg_match_all('/"appId"\:"(\d+)".*?"title"\:"(.*?)".*?/is', $stream, $matches); if ( !is_array($matches[1])) return false; $returns = array(); foreach ( $matches[1] as $key=>$val) { $temp = array(); $returns[$matches[1][$key]] = $matches[2][$key]; } return $returns; } /** * 获取平台分组 * * @return array */ public function getGroup() { $url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid=0&token='.$this->_token.'&lang=zh_CN'; $stream = $this->_html($url); // 分组 preg_match('/"groups"\:(.*?)\\}\).groups/is', $stream, $match); $jsonArr = json_decode($match[1], true); $returns = array(); foreach ( $jsonArr as $key=>$val) { $returns[$val['id']] = $val['name'].'('.$val['cnt'].')'; } return $returns; } /** * 获取分组成员 * * @param integer $gId * @return array; */ public function getFriendByGroup($gId) { $url = 'https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=10&pageidx=0&type=0&groupid='.$gId.'&token='.$this->_token.'&lang=zh_CN'; $stream = $this->_html($url); // 分析分组中好友 preg_match('/"contacts"\:(.*?)\\}\).contacts/is', $stream, $match); $jsonArr = json_decode($match[1], true); if ( !is_array($jsonArr)) return false; $returns = array(); foreach ( $jsonArr as $key=>$val) { $temp = array(); $temp['fakeId'] = $val['id']; $temp['nickName'] = $val['nick_name']; $temp['remarkName'] = $val['remark_name']; $returns[] = $temp; } return $returns; } /** * 批量按组发送 * * @param integer $gId 分组ID * @param string $content * @return array */ public function battchMesgByGroup($gId, $content) { $mebInfo = $this->getFriendByGroup($gId); if ( false == $mebInfo) return false; // 循环发送 $returns = array(); foreach ( $mebInfo as $key=>$val) { $val['content'] = $content; $this->sendmesg($val) ? $returns['succ'] ++ : $returns['err']++; } return $returns; } /** * 发送消息 * * 结构 $param = array(fakeId, content, msgId); * @param array $param * @return boolean */ public function sendmesg($param) { $url = 'https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response'; // 分类型进行推送 if ( (int)$param['content']>100000) { $post = 'error=false&tofakeid='.$param['fakeId'].'&type=10&fid='.$param['content'].'&appmsgid='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this->_token.'&ajax=1'; } else { $post = 'error=false&tofakeid='.$param['fakeId'].'&type=1&content='.$param['content'].'&quickreplyid='.$param['msgId'].'&token='.$this->_token.'&ajax=1'; } $this->_header[1] = "Referer:https://mp.weixin.qq.com/cgi-bin/singlemsgpage?msgid=&source=&count=20&t=wxm-singlechat&fromfakeid=".$param['fakeId']."&token=".$this->_token; $stream = $this->_html($url, $post); // 是不是设置成功 $html = preg_replace("/^.*\{/is", "{", $stream); $json = json_decode($html, true); return (boolean)$json['msg'] == 'ok'; } /** * 从Stream中提取cookie * * @param string $stream */ private function _cookie($stream) { preg_match_all("/Set-Cookie: (.*?);/is", $stream, $matches); $this->_cookie = @implode(";", $matches[1]); } /** * 获取Stream * * @param string $url * @param string $post * @return mixed */ private function _html($url, $post = FALSE) { ob_start(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $this->_header); curl_setopt($ch, CURLOPT_TIMEOUT, 300); if ( $post){ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIE, $this->_cookie); //curl_setopt($ch, CURLOPT_PROXY, 'http://10.100.10.100:3128'); curl_exec($ch); curl_close($ch); $_str = ob_get_contents(); $_str = str_replace("script", "", $_str); ob_end_clean(); return $_str; } /** * 获取最新消息 * * 返回结构:id:msgId; fakeId; nickName; content; * * @return array */ public function newmesg($ee) { $url='https://mp.weixin.qq.com/cgi-bin/message?t=message/list&action=&keyword=&frommsgid=200122055&offset='.$ee.'&count=20&day=7&filterivrmsg=0&token='.$this->_token; $stream = $this->_html($url); preg_match('/"msg_item"\:(.*?)\\}\).msg_item/is', $stream, $match); $jsonArr = json_decode($match[1], true); $returns = array(); foreach ( $jsonArr as $val){ if ( isset($val['is_starred_msg'])) continue; $returns[] = $val; } return $returns; } } ?>
$wx = new Weixin();
$flag = $wx->login($param);
使用这个方法的时候,如果的信息比较多,可以循环
为什么不正则匹配出page页数,我试过,接受过来之后,不知道是什么原因,老是采集超时。
$p ="看下你有多少页+1在乘以20";
for循环一下$flag = $wx->login($p );
我没循环的原因之一,就是采集超时,可能是我公司的网络比较差的原因,写得搓
只打印3个值,如果你想多打印些,可以吧对应字段全都打印出来
记住开始CURL才能运行起来