Overview

Packages

  • None
  • WxRobot
    • Admin
    • Cmd
    • CoreFunctions
    • Exends
    • Install
    • Table
    • Uninstall
    • WxSDK

Classes

  • ErrorCode
  • PKCS7Encoder
  • Prpcrypt
  • SHA1
  • Weixin_BaseCore
  • WeiXin_SDK
  • Weixin_Template
  • WXBizMsgCrypt
  • WxRobot_Admin
  • WxRobot_Admin_Menu_Extends
  • WxRobot_Admin_Menu_Instro
  • WxRobot_Admin_Menu_Menu
  • WxRobot_Admin_Menu_Records
  • WxRobot_Admin_Menu_Reply
  • WxRobot_Admin_Menu_Setting
  • WxRobot_Admin_Menu_Statistics
  • WxRobot_Cmd
  • WxRobot_Cmd_Event
  • WxRobot_Cmd_Event_User
  • WxRobot_Cmd_Text
  • WxRobot_Extends
  • WxRobot_Install
  • WxRobot_Robot
  • WxRobot_SDK
  • WxRobot_Table_Extends
  • WxRobot_Table_Menu
  • WxRobot_Table_Records
  • WxRobot_Table_Reply
  • WxRobot_Uninstall
  • WxRobot_Wp
  • XMLParse

Functions

  • wx_admin_log
  • wx_is_xml
  • wx_notice_msg
  • wx_parse_xml
  • wx_random_big_pic
  • wx_random_small_pic
  • wx_request_array
  • wx_request_decode
  • wx_request_is_encode
  • wx_request_xml
  • wx_send_encode
  • Overview
  • Package
  • Class
  1: <?php
  2: /**
  3:  * WxRobot CoreFunctions
  4:  * 
  5:  * WP微信机器人核心方法
  6:  *
  7:  * @author      midoks
  8:  * @category    CoreFunction
  9:  * @package     WxRobot/CoreFunctions
 10:  * @since       5.3.0
 11:  */
 12: 
 13: if ( ! defined( 'ABSPATH' ) ) {
 14:     exit;
 15: }
 16: 
 17: /**
 18:  * 把XML转化为数组
 19:  *
 20:  * @param string $xml XML数据
 21:  * @return array
 22:  */
 23: function wx_parse_xml($xml){
 24:     $array = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
 25:     return (Array)$array;
 26: }
 27: 
 28: /**
 29:  * 随机获取大图片地址
 30:  *
 31:  * @return url
 32:  */
 33: function wx_random_big_pic(){
 34:     return WEIXIN_ROBOT_URL.'/assets/img/640_320/'.mt_rand(1,5).'.jpg';
 35: }
 36: 
 37: /**
 38:  * 随机获取小图地址
 39:  *
 40:  * @return url
 41:  */
 42: function wx_random_small_pic(){
 43:     return WEIXIN_ROBOT_URL.'/assets/img/80_80/'.mt_rand(1,10).'.jpg';
 44: }
 45: 
 46: 
 47: /**
 48:  *  判断是否时xml数据
 49:  *
 50:  *  @param string $xml 检查数据
 51:  *  @return bool
 52:  */
 53: function wx_is_xml($xml){
 54:     if(substr($xml, 0, strlen('<xml>')) == '<xml>'){
 55:         return true;
 56:     }
 57:     return false;
 58: }
 59: 
 60: /**
 61:  * 获取微信请求的XML数据
 62:  *
 63:  * @return xml
 64:  */
 65: function wx_request_xml(){
 66: 
 67:     if(!empty($GLOBALS["HTTP_RAW_POST_DATA"])){
 68:         return $GLOBALS["HTTP_RAW_POST_DATA"];
 69:     }else{
 70:         return file_get_contents('php://input');
 71:     }
 72: }
 73: 
 74: 
 75: /**
 76:  * 检查是否是否加密 
 77:  *
 78:  * @return bool
 79:  */
 80: function wx_request_is_encode(){
 81:     $result_xml = wx_request_xml();
 82:     $result = wx_parse_xml($result_xml);
 83:     if(isset($result['Encrypt'])){
 84:         return true;
 85:     }
 86:     return false;
 87: }
 88: 
 89: 
 90: /**
 91:  * 解析微信请求的数据
 92:  *
 93:  * @return array
 94:  */
 95: function wx_request_array(){
 96:     
 97:     $options = get_option(WEIXIN_ROBOT_OPTIONS);
 98: 
 99:     //测试地址:midoks.duapp.com/?midoks&debug=1
100:     if($options['weixin_robot_debug'] == 'true' && (isset($_GET['debug']) || $_GET['debug'] == '1')){
101:         $info['MsgType'] = 'text';//text,event,
102:         $info['FromUserName'] = 'userid';
103:         $info['ToUserName'] = 'openid';
104:         $info['CreateTime'] = time();
105:         $info['Content'] = (isset($_GET['kw']))?$_GET['kw']:'?';
106:         
107:         //事件名
108:         //$info['MsgType'] = 'event';//text,event,
109:         //$info['Event'] = 'subscribe';
110:         //$info['EventKey'] = 'MENU_1444226908';
111:         //
112:         //$info['Location_X'] = 'Location_X';
113:         //$info['Location_Y'] = 'Location_Y';
114:         //$info['Scale'] = 'Scale';
115:         //$info['Label'] = 'Label';
116:         return $info;
117:     }
118: 
119:     $result_xml = wx_request_xml();
120: 
121:     if(!wx_is_xml($result_xml)){
122:         return false;
123:     }
124: 
125: 
126:     if(!empty($result_xml)){
127:         $result = wx_parse_xml($result_xml);
128:         if(isset($result['Encrypt'])){
129:             $result = wx_request_decode($result_xml);
130:             return  wx_parse_xml($result);
131:         }
132:         return $result;
133:     }
134:     return false;
135: }
136: 
137: 
138: /**
139:  * 编码发送到微信的信息
140:  *
141:  * @param xml $text 要编码的数据
142:  * @return xml
143:  */
144: function wx_send_encode($text){
145:     $token      = WEIXIN_TOKEN;
146:     $options    = get_option(WEIXIN_ROBOT_OPTIONS);
147:     
148:     $encodingAesKey = $options['EncodingAESKey'];
149:     $appId = $options['ai'];
150:     $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
151:         
152:     $timeStamp = time();
153:     $encryptMsg = '';
154:     $nonce = $_GET['nonce'];
155:     $retCode = $pc->encryptMsg($text, $timeStamp, $nonce, $encryptMsg);
156:     if($retCode == 0){
157:         return $encryptMsg;
158:     }
159:     return $text;
160: }
161: 
162: /**
163:  * 解码微信发送来的信息
164:  *
165:  * @param xml $text 要解码的数据  
166:  * @return xml
167:  */
168: function wx_request_decode($text){
169:     $token      = WEIXIN_TOKEN;
170:     $options    = get_option(WEIXIN_ROBOT_OPTIONS);
171:     $info       = wx_parse_xml($text);  
172: 
173:     $encodingAesKey = $options['EncodingAESKey'];
174:     $appId = $options['ai'];
175:     $pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);
176: 
177:     $timeStamp = time();
178:     $nonce = $_GET['nonce'];
179: 
180:     $sha1 = new SHA1;
181:     $array = $sha1->getSHA1($token, $timeStamp, $nonce, $info['Encrypt']);
182:     $ret = $array[0];
183:     if ($ret != 0) {
184:         return $ret;
185:     }
186:     $msg_sign = $array[1];
187:         
188:     $retCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $text, $msg);
189:     if($retCode == 0){
190:         return $msg;
191:     }
192:     return $text;
193: }
194: 
195: ?>
196: 
API documentation generated by ApiGen