FromXml 支付回调 xml 转数组

    public function xx(){
        $xml = '<xml><appid><![CDATA[xxxxxxxxxxxxx]]></appid>
                <bank_type><![CDATA[CFT]]></bank_type>
                <cash_fee><![CDATA[3500]]></cash_fee>
                <fee_type><![CDATA[CNY]]></fee_type>
                <is_subscribe><![CDATA[N]]></is_subscribe>
                <mch_id><![CDATA[xxxxxxxxxxxx]]></mch_id>
                <nonce_str><![CDATA[xxxxxxxxxxxxx]]></nonce_str>
                <openid><![CDATA[xxxxxxxxxxxxxxxxxxxxxxxxxx]]></openid>
                <out_trade_no><![CDATA[111400xxxxxxxxxxxxxx]]></out_trade_no>
                <result_code><![CDATA[SUCCESS]]></result_code>
                <return_code><![CDATA[SUCCESS]]></return_code>
                <sign><![CDATA[DBBB36A12ECE7E3Cxxxxxxxxxxxxxxxx]]></sign>
                <time_end><![CDATA[20191114005240]]></time_end>
                <total_fee>3500</total_fee>
                <trade_type><![CDATA[JSAPI]]></trade_type>
                <transaction_id><![CDATA[42000004xxxxxxxxxxxxxxxxxxxx3]]></transaction_id>
                </xml>';

        $data = $this->FromXml($xml);
        dump($data);
    }



    public function FromXml($xml)
    {
        if(!$xml){
            throw new WxPayException("xml数据异常!");
        }

        //将XML转为array
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $this->values;
    }


<pre>array(16) {
[&quot;appid&quot;] =&gt; string(18) &quot;xxxxxxxxxxxxxxxxxxx&quot;
[&quot;bank_type&quot;] =&gt; string(3) &quot;CFT&quot;
[&quot;cash_fee&quot;] =&gt; string(4) &quot;3500&quot;
[&quot;fee_type&quot;] =&gt; string(3) &quot;CNY&quot;
[&quot;is_subscribe&quot;] =&gt; string(1) &quot;N&quot;
[&quot;mch_id&quot;] =&gt; string(10) &quot;xxxxxxxxxxxxx&quot;
[&quot;nonce_str&quot;] =&gt; string(16) &quot;Oec5xxxxxxxxxxxxx&quot;
[&quot;openid&quot;] =&gt; string(28) &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
[&quot;out_trade_no&quot;] =&gt; string(18) &quot;111400522494411933&quot;
[&quot;result_code&quot;] =&gt; string(7) &quot;SUCCESS&quot;
[&quot;return_code&quot;] =&gt; string(7) &quot;SUCCESS&quot;
[&quot;sign&quot;] =&gt; string(32) &quot;DBBB36A12ECxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
[&quot;time_end&quot;] =&gt; string(14) &quot;20191114005240&quot;
[&quot;total_fee&quot;] =&gt; string(4) &quot;3500&quot;
[&quot;trade_type&quot;] =&gt; string(5) &quot;JSAPI&quot;
[&quot;transaction_id&quot;] =&gt; string(28) &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;
}
</pre>






原文地址:https://www.cnblogs.com/pansidong/p/11855622.html