SoapClient将xml添加到返回的字符串

时间:2018-02-09 20:21:20

标签: php json ajax xml

我使用PHP调用AJAX脚本,其数据类型为JSON,后者又调用SoapClient Web服务。

然后我使用以下命令将返回的stdClass数组转换为PHP数组

$array = json_decode(json_encode($response), true);

打印时看起来像这样:

Array
(
[QueryResult] => OK
[status] => 1
[description] => Authorised
[authorisation] => Array
    (
        [XElement] => Array
            (
                [0] => Array
                    (
                        [any] => 999777
                    )

                [1] => Array
                    (
                        [any] => 44**********1111
                    )

                [2] => Array
                    (
                        [any] => 34490704
                    )

                [3] => Array
                    (
                        [any] => 05/27
                    )

                [4] => Array
                    (
                        [any] => 1000
                    )

                [5] => Array
                    (
                        [any] => 1000
                    )

                [6] => Array
                    (
                        [any] => 0
                    )

                [7] => Array
                    (
                        [any] => 444433124490719785137937321111
                    )

                [8] => Array
                    (
                        [any] => VC
                    )

            )

    )

)

我然后json_encode将其返回到我的AJAX电话,但我在这里得到XML,例如:

<CardNo xmlns="">44**********1111</CardNo>

但我只想要卡号 - 而不是封闭标签 - 可能吗?我一整晚都在努力,我准备拔出最后一根头发!

PHP代码

try {
    $soapclient = new SoapClient($wsdl, array('trace' => 1));
    $params = array (
        'account' => $account,
        'user' => $user,
        'password' => $password,
        'id' => $id,
        'status' => $status,
        'description' => $description,
        'authorisation' => $authorisation
        );

    $response = $soapclient->Query($params);  

    $array = json_decode(json_encode($response), true);

    $data["status"] = $array["status"];
    $data["description"] = $array["description"];
    $data["AuthCode"] = $array["authorisation"]["XElement"][0]["any"];
    $data["CardNo"] = $array["authorisation"]["XElement"][1]["any"];
    $data["MPOSID"] = $array["authorisation"]["XElement"][2]["any"];
    $data["CardExpiry"] = $array["authorisation"]["XElement"][3]["any"];
    $data["Amount"] = $array["authorisation"]["XElement"][4]["any"];
    $data["Paid"] = $array["authorisation"]["XElement"][5]["any"];
    $data["Surcharge"] = $array["authorisation"]["XElement"][6]["any"];
    $data["Token"] = $array["authorisation"]["XElement"][7]["any"];
    $data["CardType"] = $array["authorisation"]["XElement"][8]["any"];


    echo json_encode($data);
}

0 个答案:

没有答案