Ejemplo n.º 1
0
 /**
  * 接受用户发送的消息
  */
 public static function receiveMsg($paramArr)
 {
     $options = array('subscribeCallback' => false, 'unsubscribeCallback' => false, 'clickCallback' => false);
     if (is_array($paramArr)) {
         $options = array_merge($options, $paramArr);
     }
     extract($options);
     $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
     if (!$postStr) {
         return array();
     }
     $outArr = array();
     $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
     self::$userOpenId = $postObj->FromUserName ? (string) $postObj->FromUserName : "";
     self::$ourWxId = $postObj->ToUserName ? (string) $postObj->ToUserName : "";
     $msgType = (string) $postObj->MsgType;
     $outArr = (array) $postObj;
     $outArr["OurWeixinId"] = (string) self::$ourWxId;
     $outArr["UserOpenId"] = (string) self::$userOpenId;
     if ('event' == $msgType) {
         #事件的处理
         switch ((string) $postObj->Event) {
             case "subscribe":
                 #订阅事件
                 if ($subscribeCallback) {
                     call_user_func($subscribeCallback, $outArr);
                 }
                 break;
             case "unsubscribe":
                 #取消订阅
                 if ($unsubscribeCallback) {
                     call_user_func($unsubscribeCallback, $outArr);
                 }
                 break;
             case "CLICK":
                 #自定义菜单点击事件
                 self::$EventKey = $postObj->EventKey ? (string) $postObj->EventKey : "";
                 $outArr["MenuKey"] = (string) self::$EventKey;
                 if ($clickCallback) {
                     call_user_func($clickCallback, $outArr);
                 }
                 break;
         }
     } else {
         #普通消息
         if ("text" == $msgType) {
             $outArr["Content"] = (string) $postObj->Content;
         } elseif ("image" == $msgType) {
             $outArr["PicUrl"] = (string) $postObj->PicUrl;
         }
         return $outArr;
     }
 }