XML訊息解析_php

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   2014   cti   

初識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}\r\n"17             ."ToUserName(發送至):{$toUsername}\r\n"18             ."CreateTime(時間):{$createtime}\r\n"19             ."MsgType(訊息類型):{$msgType}\r\n";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 "原始訊息\r\n";29             echo $postStr ;30             echo "\r\n\r\n解析結果\r\n";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}\r\n"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}\r\n"51         ."MsgId(簡訊標識號):{$msgId}";52         return $result;53     }54 }55 56 ?>

 解析結果:

(1) 菜單單擊事件訊息解析:

(2)簡訊解析:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.