1: <?php
2: 3: 4: 5: 6: 7:
8: class Weixin_BaseCore{
9:
10: 11: 12: 13: 14: 15:
16: private function get($url, $json = ''){
17: $go = curl_init();
18: curl_setopt($go, CURLOPT_URL, $url);
19:
20: curl_setopt($go, CURLOPT_MAXREDIRS, 30);
21: curl_setopt($go, CURLOPT_HEADER, 0);
22: curl_setopt($go, CURLOPT_RETURNTRANSFER, 1);
23: curl_setopt($go, CURLOPT_SSL_VERIFYPEER, false);
24: curl_setopt($go, CURLOPT_TIMEOUT, 30);
25: if(!empty($json)){
26: curl_setopt($go, CURLOPT_POST, 1);
27: curl_setopt($go, CURLOPT_POSTFIELDS ,$json);
28: }
29: $response = curl_exec($go);
30: curl_close($go);
31: return $response;
32: }
33:
34: public function getToken($app_id, $app_sercet){
35: $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$app_id}&secret={$app_sercet}";
36: return $this->get($url);
37: }
38:
39: public function pushMsgText($token, $open_id, $msg){
40: $info['touser'] = $open_id;
41: $info['msgtype'] = 'text';
42: $info['text']['content'] = $msg;
43:
44: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
45: return $this->get($url, $this->to_json($info));
46: }
47:
48: public function pushMsgImage($token, $open_id, $media_id){
49: $info['touser'] = $open_id;
50: $info['msgtype'] = 'image';
51: $info['image']['media_id'] = $media_id;
52: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
53: return $this->get($url, json_encode($info));
54: }
55:
56: public function pushMsgVoice($token, $open_id, $media_id){
57: $info['touser'] = $open_id;
58: $info['msgtype'] = 'voice';
59: $info['voice']['media_id'] = $media_id;
60: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
61: return $this->get($url, json_encode($info));
62: }
63:
64: public function pushMsgVideo($token, $open_id, $media_id, $title, $desc){
65: $info['touser'] = $open_id;
66: $info['msgtype'] = 'video';
67: $info['video']['media_id'] = $media_id;
68: $info['video']['title'] = $title;
69: $info['video']['description'] = $desc;
70: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
71: return $this->get($url, json_encode($info));
72: }
73:
74: public function pushMsgMusic($token, $open_id, $thumb_media_id, $title, $desc, $musicurl, $hqmusicurl){
75: $info['touser'] = $open_id;
76: $info['msgtype'] = 'music';
77: $info['music']['title'] = $title;
78: $info['music']['description'] = $desc;
79: $info['music']['thumb_media_id'] = $thumb_media_id;
80: $info['music']['musicurl'] = $musicurl;
81: $info['music']['hqmusicurl'] = $hqmusicurl;
82: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
83: return $this->get($url, json_encode($info));
84: }
85:
86: public function pushMsgNews($token, $open_id, $info){
87: $info['touser'] = $open_id;
88: $info['msgtype'] = 'news';
89: $info['news']['articles'] = $info;
90: $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$token}";
91: return $this->get($url, json_encode($info));
92: }
93:
94:
95: 96: 97: 98:
99:
100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public function getCustomServiceLog($token, $open_id, $starttime, $endtime, $pagesize=20, $pageindex=1){
110: $url = "https://api.weixin.qq.com/cgi-bin/customservice/getrecord?access_token={$token}";
111: $info['open_id'] = $open_id;
112: $info['starttime'] = $starttime;
113: $info['endtime'] = $endtime;
114: $info['pagesize'] = $pagesize;
115: $info['pageindex'] = $pageindex;
116: return $this->get($url, json_encode($info));
117: }
118:
119:
120:
121:
122:
123: public function menuGet($token){
124: $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token={$token}";
125: return $this->get($url);
126: }
127:
128:
129: public function menuSet($token, $json){
130: $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$token}";
131: return $this->get($url, $json);
132: }
133:
134:
135: public function menuDel($token){
136: $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={$token}";
137: return $this->get($url);
138: }
139:
140:
141:
142:
143: 144: 145: 146:
147: public function getSemantic($token, $query, $category, $lat, $long, $city, $region, $appid, $uid){
148: $url = "https://api.weixin.qq.com/semantic/semproxy/search?access_token={$token}";
149: $info['query'] = $query;
150: $info['category'] = $category;
151: $info['lat'] = $lat;
152: $info['long'] = $long;
153: $info['city'] = $city;
154: $info['region'] = $region;
155: $info['appid'] = $appid;
156: $info['uid'] = $uid;
157: return $this->get($url, json_encode($info));
158: }
159:
160:
161:
162:
163:
164:
165: public function download($token, $media_id){
166: $url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$token}&media_id={$media_id}";
167: return $this->get($url);
168: }
169:
170: public function upload($token, $type, $file){
171: $info['media'] = '@'.$file;
172: $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
173:
174: return $this->get($url, $info);
175: }
176:
177: public function uploadUrl($token, $type, $fn, $mime, $content){
178: $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}";
179: return $this->uploadContents($url, $fn, $mime, $content, $token, $type);
180: }
181:
182:
183: public function uploadContents($url, $fn, $mime, $content, $token, $type){
184: $boundary = substr(md5(rand(0,32000)), 0, 10);
185: $boundary = '--WebKitFormBoundary'.$boundary;
186:
187: $data .= "--$boundary\n";
188: $data .= "Content-Disposition: form-data; name=\"media\"; filename=\"{$fn}\";\r\n";
189: $data .= 'Content-Type: '.$mime."\r\n";
190: $data .= $content."\r\n\r\n";
191: $data .= "--$boundary--\r\n";
192:
193: return $this->get($url, $data);
194:
195: }
196:
197: public function uploadContent5($url, $fn, $mime, $content, $token, $type){
198: $boundary = substr(md5(rand(0,32000)), 0, 10);
199: $boundary = '--WebKitFormBoundary'.$boundary;
200:
201:
202:
203:
204:
205: $data .= "--$boundary\n";
206: $data .= "Content-Disposition: form-data; name=\"media\"; filename=\"{$fn}\";\r\n";
207: $data .= 'Content-Type: '.$mime."\r\n\r\n\r\n\r\n";
208: $data .= $content."\r\n\r\n";
209: $data .= "--$boundary--\r\n";
210:
211:
212: $fp = fsockopen('file.api.weixin.qq.com', 80, $errno, $errstr, 10);
213:
214: $postStr = "POST /cgi-bin/media/upload?access_token={$token}&type={$type} HTTP/1.1\r\n";
215:
216:
217: $postStr .= "Host: file.api.weixin.qq.com\r\n";
218: $postStr .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
219: $postStr .= "Content-Length: ".strlen(trim($data))."\r\n";
220: $postStr .= "Content-Type: multipart/form-data; boundary={$boundary}\r\n";
221: $postStr .= "Accept-Encoding: gzip,deflate,sdch;\r\n";
222:
223:
224: 225: 226: 227: 228:
229: $postStr .= "\r\n\r\n";
230:
231: $postStr .= $data;
232:
233: echo '<pre>';
234: echo $postStr;
235: echo "</pre>";
236:
237: if($fp){
238: fwrite($fp, $postStr);
239: while (!feof($fp)) {
240: echo fgets($fp, 128);
241: }
242: }else{
243: return false;
244: }
245:
246: }
247:
248: private function uploadContents4($url, $fn, $mime, $content){
249: $boundary = substr(md5(rand(0,32000)), 0, 10);
250:
251:
252:
253:
254:
255: $data .= "--$boundary\n";
256: $data .= "Content-Disposition: form-data; name=\"media\"; filename=\"{$fn}\"\r\n";
257: $data .= 'Content-Type: '.$mime."\r\n";
258: $data .= 'Content-Transfer-Encoding: binary'."\r\n\r\n\r\n";
259: $data .= ($content)."\r\n";
260: $data .= "--$boundary--\r\n";
261:
262: echo '<pre>';
263: echo $data;
264: echo '</pre>';
265:
266: $context = stream_context_create(array(
267: 'http'=> array(
268: 'method' => 'POST',
269: 'timeout'=> 10,
270: 'user_agent'=>$_SERVER['HTTP_USER_AGENT'],
271: 'header' =>"Content-Type: multipart/form-data; boundary={$boundary}".
272: "\r\nContent-Length: ".strlen($content).
273: "\r\nReferer: http://mp.weixin.qq.com/".
274: "\r\n\r\n",
275: 'content'=> $data
276: )
277: ));
278: $ret = file_get_contents($url, false, $context);
279: return $ret;
280: }
281:
282: private function uploadContents1($url, $fn, $mime, $content){
283:
284: $info['media'] = '@'.$content;
285: $temp_headers = array(
286: "Content-Disposition: attachment; form-data; name=\"media\";filename='{$fn}'",
287: 'Content-Type: application/x-www-form-urlencoded',
288: 'Content-Length: '.strlen($content),
289: );
290:
291: $go = curl_init();
292: curl_setopt($go, CURLOPT_URL, $url);
293: curl_setopt($go, CURLOPT_HTTPHEADER, $temp_headers);
294: curl_setopt($go, CURLOPT_FOLLOWLOCATION, 1);
295: curl_setopt($go, CURLOPT_MAXREDIRS, 30);
296: curl_setopt($go, CURLOPT_HEADER, 0);
297: curl_setopt($go, CURLOPT_RETURNTRANSFER, 1);
298: curl_setopt($go, CURLOPT_SSL_VERIFYPEER, false);
299: curl_setopt($go, CURLOPT_TIMEOUT, 30);
300:
301:
302: curl_setopt($go, CURLOPT_POST, 1);
303: curl_setopt($go, CURLOPT_POSTFIELDS, $content);
304: $response = curl_exec($go);
305: curl_close($go);
306: return $response;
307: }
308:
309: private function uploadContents3($url, $fn, $mime, $content){
310: $boundary = substr(md5(rand(0,32000)), 0, 10);
311:
312: $data .= "--$boundary\n";
313: $data .= "Content-Disposition: form-data; name=\"media\"\n\n";
314: $data .= "media\n";
315:
316: $data .= "--$boundary\n";
317: $data .= "Content-Disposition: form-data; name=\"media\";filename=\"{$fn}\"\n";
318: $data .= 'Content-Type: '.$mime."\n";
319: $data .= 'Content-Transfer-Encoding: binary'."\n\n";
320: $data .= $content."\n";
321: $data .= "--$boundary--\n";
322:
323: $go = curl_init();
324: curl_setopt($go, CURLOPT_URL, $url);
325: curl_setopt($go, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=".$boundary,
326: 'Referer: https://mp.weixin.qq.com'));
327: curl_setopt($go, CURLOPT_FOLLOWLOCATION, 1);
328: curl_setopt($go, CURLOPT_MAXREDIRS, 30);
329: curl_setopt($go, CURLOPT_HEADER, 0);
330: curl_setopt($go, CURLOPT_RETURNTRANSFER, 1);
331: curl_setopt($go, CURLOPT_SSL_VERIFYPEER, false);
332: curl_setopt($go, CURLOPT_TIMEOUT, 30);
333:
334:
335: curl_setopt($go, CURLOPT_POST, 1);
336: curl_setopt($go, CURLOPT_POSTFIELDS, $data);
337: $response = curl_exec($go);
338: curl_close($go);
339: return $response;
340: }
341:
342:
343: public function getUserInfo($token, $open_id, $lang='zh_CN'){
344:
345: $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$token}&openid={$open_id}lang={$lang}";
346: return $this->get($url);
347: }
348:
349: public function getUserList($token, $next_openid){
350: if(empty($open_id)){
351: $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}";
352: }else{
353: $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$token}&next_openid={$next_openid}";
354: }
355: return $this->get($url);
356: }
357:
358: public function setUserGroup($token, $json){
359: $url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token={$token}";
360: return $this->get($url, $json);
361: }
362:
363: public function getUserGroup($token){
364: $url = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token={$token}";
365: return $this->get($url);
366: }
367:
368: public function getUserGroupPosition($token, $json){
369: $url = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={$token}";
370: return $this->get($url, $json);
371: }
372:
373: public function modUserGroup($token, $json){
374: $url = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token={$token}";
375: return $this->get($url, $json);
376: }
377:
378: public function movUserGroup($token, $json){
379: $url = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={$token}";
380: return $this->get($url, $json);
381: }
382:
383: public function updateRemark($token, $open_id, $remark){
384: $url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token={$remark}";
385: $a['openid'] = $open_id;
386: $a['remark'] = $remark;
387: return $this->get($url, $this->to_json($a));
388: }
389:
390:
391:
392:
393:
394:
395:
396: public function temp_ticket($token, $json){
397: $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$token}";
398: return $this->get($url, $this->to_json($json));
399: }
400:
401:
402: public function permanent_ticket($token, $json){
403: $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token={$token}";
404: return $this->get($url, $this->to_json($json));
405: }
406:
407:
408: public function get_ticket($ticket){
409: $url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticket;
410: return $url;
411: }
412:
413:
414: public function long2short($token, $url){
415: $array['access_token'] = $token;
416: $array['long_url'] = $url;
417: $array['action'] = 'long2short';
418: return $this->get("https://api.weixin.qq.com/cgi-bin/shorturl?access_token={$token}", $array);
419: }
420:
421:
422:
423:
424: public function uploadMsgImageText($token, $msg){
425: $url = 'https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token='.$token;
426: $info['articles'] = $msg;
427: return $this->get($url, $this->to_json($info));
428: }
429:
430:
431: public function sendAllByGroup($token, $group_id, $media_id, $msgtype = 'mpnews'){
432: $msg['filter']['group_id'] = $group_id;
433: $msg['mpnews']['media_id'] = $media_id;
434: $msg['msgtype'] = $msgtype;
435: $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.$token;
436: return $this->get($url, $this->to_json($msg));
437: }
438:
439:
440: public function sendAll($token, $user, $media_id, $msgtype = 'mpnews'){
441: $msg['touser'] = $user;
442: $msg['mpnews']['media_id'] = $media_id;
443: $msg['msgtype'] = $msgtype;
444: $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$token;
445: return $this->get($url, $this->to_json($msg));
446: }
447:
448:
449: public function deleteSend($token, $id){
450: $msg['msgid'] = $id;
451: $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete?access_token='.$token;
452: return $this->get($url, $this->to_json($msg));
453: }
454:
455:
456: public function sendTemplateInfo($token, $json){
457: $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
458: return $this->get($url, $this->to_json($json));
459: }
460:
461:
462: public function getWeixinIp($token){
463: $url = 'https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token='.$token;
464: return $this->get($url);
465: }
466:
467:
468: public function to_json($array){
469: $this->arrayRecursive($array, 'urlencode', true);
470: $json = json_encode($array);
471: return urldecode($json);
472: }
473:
474: 475: 476: 477: 478: 479: 480: 481: 482:
483: public function arrayRecursive(&$array, $function, $apply_to_keys_also = false){
484: static $recursive_counter = 0;
485: if (++$recursive_counter > 1000) {
486: die('possible deep recursion attack');
487: }
488: foreach ($array as $key => $value) {
489: if (is_array($value)) {
490: $this->arrayRecursive($array[$key], $function, $apply_to_keys_also);
491: } else {
492: $array[$key] = $function($value);
493: }
494: if ($apply_to_keys_also && is_string($key)) {
495: $new_key = $function($key);
496: if ($new_key != $key) {
497: $array[$new_key] = $array[$key];
498: unset($array[$key]);
499: }
500: }
501: }
502: $recursive_counter--;
503: }
504: }
505: ?>
506: