PHP接收并处理请求中携带的xml格式的信息
企业微信以xml的形式给服务器发送通知。具体参考:企业微信文档——事件格式
dump(file_get_contents("php://input"));
/**
*
string(530) "<xml>
<ToUserName><![CDATA[ww55ca070cb9b7eb22]]></ToUserName>
<FromUserName><![CDATA[sys]]></FromUserName>
<CreateTime>1403610513</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[change_external_chat]]></Event>
<ChatId><![CDATA[wrx7HUARsKwGRaQBVKPBTcEyzdHA4HrQ]]></ChatId>
<ChangeType><![CDATA[update]]></ChangeType>
<UpdateDetail><![CDATA[add_member]]></UpdateDetail>
<JoinScene>1</JoinScene>
<QuitScene>0</QuitScene>
<MemChangeCnt>10</MemChangeCnt>
</xml>"
*/
$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"
}
*
*/