接收文件并转成xml格式
$xml_str = file_get_contents('php://input');
$content_type = filter_input(INPUT_SERVER, "CONTENT_TYPE");
switch ($content_type) {
case 'application/x-compress-zlib' :
$xml_str = gzuncompress($xml_str);
$compressmode = "zlib";
break;
case 'Content-Type: application/x-compress-gzip' :
$xml_str = gzdecode($xml_str);
$compressmode = "gzip";
break;
default :
$compressmode = "none";
}
将xml格式字符串转成simplexml对象,再将simplexml对象转成数组
$xml = @simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
if (!$xml) {
$return_xml->addChild('ERROR','XML结构不完整!');
} else {
$array = json_decode(json_encode($xml), true);
}
创建返回的simplexml对象
$return_xml = "<?xml version='1.0' encoding='UTF-8'?>
<REPLY>
</REPLY>";
$return_xml = @simplexml_load_string($return_xml, 'SimpleXMLElement', LIBXML_NOCDATA);
向返回的simplexml对象添加key-value
$return_xml->addChild($key, $val);
$return_xml->addChild('ERROR','参数未知!');
返回相应格式xml文件
// 返回xml文件
switch ($compressmode) {
case 'none' :
header("Content-Type: application/xml");
echo $return_xml->asXML();
break;
case 'zlib' :
header("Content-Type: application/x-compress-zlib");
echo gzcompress($return_xml->asXML());
break;
case 'gzip' :
header("Content-Type: application/x-compress-gzip");
echo gzencode($return_xml->asXML());
break;
default :
echo "未知文件格式!";
}