0

I'm calling a PHP script using AJAX with a dataType of JSON which in turn calls a SoapClient Webservice.

I then convert the returned stdClass array to a PHP array using:

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

Which looks like this when printed:

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
                    )

            )

    )

)

I then json_encode it and return it to my AJAX call but I'm getting XML in there like this for example:

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

But I only want the Card number - not the enclosing tags - is that possible? I've been trying all night and I'm about to pull the last of my hair out!

PHP Code

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);
}
Michael Bellamy
  • 543
  • 1
  • 8
  • 16
  • When you get your response from the soap client, do you use a soap parser first before trying to json_encode it? Try using simplexml. Here's a good example: https://stackoverflow.com/questions/8830599/php-convert-xml-to-json – Senica Gonzalez Feb 09 '18 at 20:31
  • Thanks, I've tried that but it's giving me an empty array. I'll update my question to include my PHP code. – Michael Bellamy Feb 09 '18 at 20:35

0 Answers0