PHP接收并处理请求中携带的xml格式的信息

企业微信以xml的形式给服务器发送通知。具体参考:企业微信文档——事件格式

        dump(file_get_contents("php://input"));
        /**
         *
         string(530) "
        
        
        1403610513
        
        
        
        
        
        1
        0
        10
        "
         */
        $xml = simplexml_load_string(file_get_contents("php://input"), 'SimpleXMLElement', LIBXML_NOCDATA);
        dump($xml);
        /**
         *object(SimpleXMLElement)#22 (11) {
        ["ToUserName"] => string(18) "ww55ca070cb9b7eb22"
        ["FromUserName"] => string(3) "sys"
        ["CreateTime"] => string(10) "1403610513"
        ["MsgType"] => string(5) "event"
        ["Event"] => string(20) "change_external_chat"
        ["ChatId"] => string(32) "wrx7HUARsKwGRaQBVKPBTcEyzdHA4HrQ"
        ["ChangeType"] => string(6) "update"
        ["UpdateDetail"] => string(10) "add_member"
        ["JoinScene"] => string(1) "1"
        ["QuitScene"] => string(1) "0"
        ["MemChangeCnt"] => string(2) "10"
        }
         *
         */
        $data = array();
        foreach ($xml as $k => $v) {
            $data[(string) $k] = (string) $v;
        }

        dump($data);
        /**
         * array(11) {
        ["ToUserName"] => string(18) "ww55ca070cb9b7eb22"
        ["FromUserName"] => string(3) "sys"
        ["CreateTime"] => string(10) "1403610513"
        ["MsgType"] => string(5) "event"
        ["Event"] => string(20) "change_external_chat"
        ["ChatId"] => string(32) "wrx7HUARsKwGRaQBVKPBTcEyzdHA4HrQ"
        ["ChangeType"] => string(6) "update"
        ["UpdateDetail"] => string(10) "add_member"
        ["JoinScene"] => string(1) "1"
        ["QuitScene"] => string(1) "0"
        ["MemChangeCnt"] => string(2) "10"
        }
         * 
         */

你可能感兴趣的:(xml)