Example #1
0
File: api.php Project: noikiy/snk
 public function dopost($post_xml)
 {
     $post_arr = vmc::singleton('mobile_utility_xml')->xml2array($post_xml);
     $post_data = $post_arr['xml'];
     //公众账号ID获取
     $wechat_id = $post_data['ToUserName'];
     $bind = app::get('wechat')->model('bind')->getRow('*', array('wechat_id' => $wechat_id, 'status' => 'active'));
     //需要解密消息
     if ($_GET['encrypt_type'] == 'aes') {
         $obj_crypt = new wechat_crypt($bind['token'], $bind['aeskey'], $bind['appid']);
         if ($obj_crypt->decryptMsg($_GET['msg_signature'], $_GET["timestamp"], $_GET["nonce"], $post_xml, $decode_post_xml) === 0) {
             $post_arr = vmc::singleton('mobile_utility_xml')->xml2array($decode_post_xml);
             $post_data = $post_arr['xml'];
         } else {
             logger::error('微信消息解密失败!' . $post_xml . var_export($_GET, 1) . var_export($bind, 1));
             return;
         }
     }
     if (!empty($bind)) {
         $post_data['bind_id'] = $bind['id'];
         $post_data['eid'] = $bind['eid'];
     } else {
         return;
     }
     //logger::alert(var_export($post_data,1));
     switch ($post_data['MsgType']) {
         case 'event':
             /**
              * subscribe(订阅)、unsubscribe(取消订阅)
              * scan 带参数二维码事件
              * location 上报地理位置事件
              * click 自定义菜单事件
              * view  点击菜单跳转链接时的事件推送
              *
              */
             $this->stage->event_reply($post_data);
             break;
         default:
             $this->stage->normal_reply($post_data);
     }
 }
Example #2
0
 /**
  * 发送消息.
  */
 public function echo_msg($data, $bind)
 {
     $data['CreateTime'] = time();
     $post_xml = vmc::singleton('mobile_utility_xml')->array2xml($data, 'xml');
     $obj_crypt = new wechat_crypt($bind['token'], $bind['aeskey'], $bind['appid']);
     $flag = $obj_crypt->encryptMsg($post_xml, $data['CreateTime'], $this->randomkeys(5), $encode_post_xml);
     if ($flag === 0) {
         echo $encode_post_xml;
     } else {
         logger::error('回应消息时,加密失败!ERROR_CODE:' . $flag . $post_xml . var_export($data, 1) . var_export($bind, 1));
         return;
     }
 }