源码:特别适用于微信支付中通知微信支付网关
function array2xml($arr, $level = 1) {
$s = $level == 1 ? "<xml>" : '';
foreach ($arr as $tagname => $value) {
//元素为数组或者不为数组的处理
if (!is_array($value)) {
$s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
} else {
$s .= "<{$tagname}>" . array2xml($value, $level + 1) . "</{$tagname}>";
}
}
//过滤不合法的字符串
$s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
return $level == 1 ? $s . "</xml>" : $s;
}
//微信支付通知后告诉微信网关,我收到了!
$data=array(
'return_code'=>'SUCCESS',
'return_msg' =>'OK',
);
$xmlstr=array2xml($data);