-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_weixin.php
190 lines (180 loc) · 7.75 KB
/
class_weixin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
define('COOKIE',dirname(__FILE__)."/cookie.txt");
class WX_Remote_Opera{
private $token;
public function init($user,$password){ //初始化,登录微信平台
$url="https://mp.weixin.qq.com/cgi-bin/login?lang=zh_CN";
$ch=curl_init($url);
$post['username']=$user;
$post['pwd']=md5($password);
$post['f']='json';
$post['imgcode']='';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证证书
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); //验证HOST
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //对推送来的消息进行设置 1不自动输出任何内容 0输出返回的内容
curl_setopt($ch,CURLOPT_HEADER,1); //是否返回头文件
curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); //数据传输最大允许时间
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_COOKIEJAR,COOKIE);
$html=curl_exec($ch);
// echo $html;
preg_match('/[\?\&]token=(\d+)"/',$html,$t); //匹配到token
$token=$t[1];
curl_close($ch);
$this->token=$token;
echo $token;
return $token;
}
public function sendmsg($content,$fromfakeid,$token){ //发送消息给指定人
$url="https://mp.weixin.qq.com/cgi-bin/singlesend";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE);
curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token='.$this->token.'&lang=zh_CN');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$post['t']='ajax-response';
$post['imgcode']='';
$post['mask']=false;
$post['random']=math_random();
$post['lang']='zh_CN';
$post['tofakeid']=$fromfakeid;
$post['type'] =1;
$post['content']=$content;
$post['token']=$this->token;
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$html=curl_exec($ch);
curl_close($ch);
}
/*发送图文消息*/
public function setimgmsg($appid,$fromfakeid){
$url="https://mp.weixin.qq.com/cgi-bin/singlesend";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE);
curl_setopt($ch,CURLOPT_REFERER,'https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token='.$this->token.'&lang=zh_CN');
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$post['t']='ajax-response';
$post['imgcode']='';
$post['mask']=false;
$post['random']=math_random();
$post['lang']='zh_CN';
$post['tofakeid']=$fromfakeid;
$post['type'] =10;
$post['content']='';
$post['appmsgid'] = $appid;
$post['token']=$this->token;
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$html=curl_exec($ch);
curl_close($ch);
}
/**获取图文消息素材**/
public function getimgmaterial(){
$url="https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&t=media/appmsg_list&type=10&action=list&token=".$this->token."&lang=zh_CN";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$html=curl_exec($ch);
curl_close($ch);
preg_match('%(?<=item\"\:)(.*)(?=,\"file_cnt)%', $html, $result);
return json_decode($result[1],true);
}
public function getcontactinfo($fromfakeid){
$url="https://mp.weixin.qq.com/cgi-bin/getcontactinfo";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$post['t']='ajax-getcontactinfo';
$post['fakeid'] =$fromfakeid;
$post['token']=$this->token;
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$html=curl_exec($ch);
curl_close($ch);
$arr=json_decode($html,true);
return $arr['contact_info'];
}
// public function getheadimg($fromfakeid){
// $url="https://mp.weixin.qq.com/cgi-bin/getheadimg";
// $ch=curl_init($url);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
// curl_setopt($ch,CURLOPT_REFERER,$url);
// curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
// $post['fakeid'] =$fromfakeid;
// $post['token']=$this->token;
// curl_setopt($ch,CURLOPT_POST,1);
// curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
// $headimg=curl_exec($ch);
// curl_close($ch);
// //$PNG_SAVE_DIR = S_ROOT.'uploads'.DIRECTORY_SEPARATOR.'weixin_headimg'.DIRECTORY_SEPARATOR;
// //$file = fopen($PNG_SAVE_DIR.$fromfakeid.".png","w");//打开文件准备写入
// //fwrite($file,$headimg);//写入
// //fclose($file);//关闭
// echo $headimg;
// }
public function getcontactlist($pagesize,$page){
$url="https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=".$pagesize."&pageidx=".$page."&type=0&groupid=0&token=".$this->token."&lang=zh_CN";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$html=curl_exec($ch);
curl_close($ch);
preg_match('%(?<=\"contacts\"\:)(.*)(?=}\)\.contacts)%', $html, $result);
print_r($result);
return json_decode($result[1],true);
}
/*获取各分组人数*/
public function getsumcontactlist(){
$url="https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=0&pageidx=0&type=0&groupid=0&token=".$this->token."&lang=zh_CN";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$html=curl_exec($ch);
curl_close($ch);
preg_match('%(?<=\"groups\"\:)(.*)(?=}\)\.groups)%', $html, $result);
return json_decode($result[1],true);
}
public function getmsglist(){
$url="https://mp.weixin.qq.com/cgi-bin/message?t=message/list&count=20&day=7&token=".$this->token."&lang=zh_CN";
$ch=curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE,COOKIE);
curl_setopt($ch,CURLOPT_REFERER,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0');
$html=curl_exec($ch);
preg_match('%(?<=\"msg_item\"\:)(.*)(?=}\)\.msg_item)%', $html, $result);
curl_close($ch);
return json_decode($result[1],true);
}
}
function math_random () {
return (float)rand()/(float)getrandmax();
}
?>