zoukankan      html  css  js  c++  java
  • XML消息解析_php

    初识php——微信消息处理

     1 <?php
     2 
     3 $test = new weixin();
     4 $test->Message();
     5 
     6 class weixin{
     7     public function Message(){
     8         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
     9         if (!empty($postStr)){
    10             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
    11             $fromUsername = $postObj->FromUserName;
    12             $toUsername = $postObj->ToUserName;
    13             $createTime = trim($postObj->CreateTime);
    14             $createtime = date('Y/m/d H:i:s', $createTime); 
    15             $msgType = trim($postObj->MsgType);
    16             $result = "FromUserName(来自):{$fromUsername}
    "
    17             ."ToUserName(发送至):{$toUsername}
    "
    18             ."CreateTime(时间):{$createtime}
    "
    19             ."MsgType(消息类型):{$msgType}
    ";
    20             switch ($msgType){
    21                 case "event":
    22                     $result = $result.$this->receiveEvent($postObj);
    23                     break;
    24                 case "text":
    25                     $result = $result.$this->receiveText($postObj);
    26                     break;
    27             }
    28             echo "原始消息
    ";
    29             echo $postStr ;
    30             echo "
    
    解析结果
    ";
    31             echo $result;
    32         }
    33         else{
    34             echo "";
    35             exit;
    36         }
    37     }
    38     
    39     private function receiveEvent($postObj){
    40         $event =  $postObj->Event;
    41         $eventKey = $postObj->EventKey;
    42         $result = "Event(事件):{$event}
    "
    43         ."EventKey(事件值):{$eventKey}";
    44         return $result;
    45     }
    46     
    47     private function receiveText($postObj){
    48         $keyword = trim($postObj->Content);    
    49         $msgId = $postObj->MsgId;
    50         $result = "Content(文本内容):{$keyword}
    "
    51         ."MsgId(文本消息标识号):{$msgId}";
    52         return $result;
    53     }
    54 }
    55 
    56 ?>

     解析结果:

    (1) 菜单单击事件消息解析:

    (2)文本消息解析:

  • 相关阅读:
    【Nginx】Nginx性能优化及配置文件
    【算法】常见算法分类和思想
    【PHP】php位运算及其高级应用
    【数据结构】数据结构-图的基本概念
    【Redis】Redis缓存穿透解决方案之布隆过滤器
    【Linux】Linux系统5种IO模型
    【linux】/dev/null作用和/dev/random
    【Linux】Linux查找功能
    【算法】算法复杂度
    Docker Hub公共镜像仓库的使用
  • 原文地址:https://www.cnblogs.com/lahblogs/p/3901599.html
Copyright © 2011-2022 走看看