Вложенный XML-ответ soap PHP


Хорошо, я у кирпичной стены... ПОЭТОМУ я успешно делаю запрос к веб-сервису на php, я могу получить ответ и использовать php.net я могу вывести результат/ответ в удобочитаемой форме

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetLoanDetailResponse xmlns="http://vendorsite.com/">
<GetLoanDetailResult>&lt;WEB&gt;&lt;LOANDETAIL&gt;&lt;acctrefno&gt;31415&lt;/acctrefno&gt;&lt;master_acctrefno&gt;0&lt;/master_acctrefno&gt;&lt;loan_number&gt;TEST100001&lt;/loan_number&gt;&lt;name&gt;HILL ALAN&lt;/name&gt;&lt;shortname&gt;ALAN HILL&lt;/shortname&gt;&lt;current_payoff_balance&gt;14,358.83&lt;/current_payoff_balance&gt;&lt;curr_date&gt;07/14/2015&lt;/curr_date&gt;&lt;curr_maturity_date&gt;07/14/2021&lt;/curr_maturity_date&gt;&lt;interest_accrued_thru_date&gt;06/06/2017&lt;/interest_accrued_thru_date&gt;&lt;current_note_amount&gt;10,000.00&lt;/current_note_amount&gt;&lt;current_principal_balance&gt;10,000.00&lt;/current_principal_balance&gt;&lt;current_interest_balance&gt;4,358.83&lt;/current_interest_balance&gt;&lt;current_fees_balance&gt;0.00&lt;/current_fees_balance&gt;&lt;current_late_charge_balance&gt;0.00&lt;/current_late_charge_balance&gt;&lt;current_perdiem&gt;6.30&lt;/current_perdiem&gt;&lt;current_interest_rate&gt;23.00000&lt;/current_interest_rate&gt;&lt;total_past_due_balance&gt;5,651.05&lt;/total_past_due_balance&gt;&lt;total_current_due_balance&gt;5,651.05&lt;/total_current_due_balance&gt;&lt;next_billing_date&gt;06/18/2017&lt;/next_billing_date&gt;&lt;days_past_due&gt;662&lt;/days_past_due&gt;&lt;current_pending&gt;0&lt;/current_pending&gt;&lt;current_impound_balance&gt;0&lt;/current_impound_balance&gt;&lt;last_payment_date&gt;05/18/2017&lt;/last_payment_date&gt;&lt;last_payment_amount&gt;5&lt;/last_payment_amount&gt;&lt;status_code&gt;ACTIVE&lt;/status_code&gt;&lt;loan_type&gt;Term&lt;/loan_type&gt;&lt;open_date&gt;07/14/2015&lt;/open_date&gt;&lt;last_activity_date&gt;05/18/2017&lt;/last_activity_date&gt;&lt;current_udf1_balance&gt;0.00&lt;/current_udf1_balance&gt;&lt;current_udf2_balance&gt;0.00&lt;/current_udf2_balance&gt;&lt;current_udf3_balance&gt;0.00&lt;/current_udf3_balance&gt;&lt;current_udf4_balance&gt;0.00&lt;/current_udf4_balance&gt;&lt;current_udf5_balance&gt;0.00&lt;/current_udf5_balance&gt;&lt;current_udf6_balance&gt;0.00&lt;/current_udf6_balance&gt;&lt;current_udf7_balance&gt;0.00&lt;/current_udf7_balance&gt;&lt;current_udf8_balance&gt;0.00&lt;/current_udf8_balance&gt;&lt;current_udf9_balance&gt;0.00&lt;/current_udf9_balance&gt;&lt;current_udf10_balance&gt;0.00&lt;/current_udf10_balance&gt;&lt;current_suspense_balance&gt;0.00&lt;/current_suspense_balance&gt;&lt;interest_method&gt;SI&lt;/interest_method&gt;&lt;term_char&gt;Payments&lt;/term_char&gt;&lt;term&gt;72&lt;/term&gt;&lt;term_due&gt;72&lt;/term_due&gt;&lt;/LOANDETAIL&gt;&lt;/WEB&gt;</GetLoanDetailResult>
</GetLoanDetailResponse>
</soap:Body>
</soap:Envelope>

Что делается с помощью: echo "ОТВЕТ:\n". htmlentities(str_ireplace('><', ">\n<", $SoapClient->__getlastresponse())). "\n";

Я пробовал simplexml_load_string() не будет читать ответ soap с "soap:" в тегах

И

Как для детализации пространств имен SOAP с помощью SimpleXML и PHP

Пытаюсь Как преобразовать ответ SOAP в массив PHP? снова

И ВСЕ, что я могу найти в переполнении стека и на php.net / google. Я продолжаю видеть ссылки на ошибку https://bugs.php.net/bug.php?id=48966 который я сейчас пытаюсь использовать для решения этой проблемы... Я действительно не хочу использовать dom, похоже, должен быть единственный способ прочитать этот ответ на php... странно, что когда я звоню $SoapClient->__Получить ответ()) Я получаю

<WEB><LOANDETAIL><acctrefno>31415</acctrefno><master_acctrefno>0</master_acctrefno><loan_number>TEST100001</loan_number><name>Last Name F Name</name><shortname>Name</shortname><current_payoff_balance>14,358.83</current_payoff_balance><curr_date>07/14/2015</curr_date><curr_maturity_date>07/14/2021</curr_maturity_date><interest_accrued_thru_date>06/06/2017</interest_accrued_thru_date><current_note_amount>10,000.00</current_note_amount><current_principal_balance>10,000.00</current_principal_balance><current_interest_balance>4,358.83</current_interest_balance><current_fees_balance>0.00</current_fees_balance><current_late_charge_balance>0.00</current_late_charge_balance><current_perdiem>6.30</current_perdiem><current_interest_rate>23.00000</current_interest_rate><total_past_due_balance>5,651.05</total_past_due_balance><total_current_due_balance>5,651.05</total_current_due_balance><next_billing_date>06/18/2017</next_billing_date><days_past_due>662</days_past_due><current_pending>0</current_pending><current_impound_balance>0</current_impound_balance><last_payment_date>05/18/2017</last_payment_date><last_payment_amount>5</last_payment_amount><status_code>ACTIVE</status_code><loan_type>Term</loan_type><open_date>07/14/2015</open_date><last_activity_date>05/18/2017</last_activity_date><current_udf1_balance>0.00</current_udf1_balance><current_udf2_balance>0.00</current_udf2_balance><current_udf3_balance>0.00</current_udf3_balance><current_udf4_balance>0.00</current_udf4_balance><current_udf5_balance>0.00</current_udf5_balance><current_udf6_balance>0.00</current_udf6_balance><current_udf7_balance>0.00</current_udf7_balance><current_udf8_balance>0.00</current_udf8_balance><current_udf9_balance>0.00</current_udf9_balance><current_udf10_balance>0.00</current_udf10_balance><current_suspense_balance>0.00</current_suspense_balance><interest_method>SI</interest_method><term_char>Payments</term_char><term>72</term><term_due>72</term_due></LOANDETAIL></WEB>

Который я не смог разнести в массив...

Обратите внимание на GetLoanDetailResult, когда я пытаюсь что-либо сделать с помощью jsonencode/decode, я получаю ключ с этим значением и жало с остальными данными в строке без разделителей...

Конечная ЦЕЛЬ состоит в том, чтобы поместить это в массив, чтобы я мог извлекать значения и использовать их для заполнения параметров для новых переменных.

Использование php7.0 на сервере ubuntu

Код для взаимодействовать с ответом после Как преобразовать ответ SOAP в массив PHP?

$loanDetail = var_dump($loanUpdate);
         $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soapClient->__getLastResponse());
         $xml = new SimpleXMLElement($response);
         $body = $xml->xpath('//SBody');

         var_export($xml);
        echo "======================<br />";
         var_export($body);
         echo "======================<br />";

         $array = json_decode(json_encode((array)$body), TRUE); 
         print_r($array);

Возвращает

    object(stdClass)#3 (1) {
  ["NLSGetLoanDetailResult"]=>
  string(24) "7"
}
SimpleXMLElement::__set_state(array(
   'soapBody' => 
  SimpleXMLElement::__set_state(array(
     'NLSGetLoanDetailResponse' => 
    SimpleXMLElement::__set_state(array(
       'NLSGetLoanDetailResult' => '7',
    )),
  )),
))======================
array (
)======================
Array
(
)
Author: Allen Craig, 2017-06-08

2 answers

Будет ли это делать то, что вы ищете? (Собрано вместе из: https://stackoverflow.com/a/30733071/1207539)

Начиная с вашего $response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soapClient->__getLastResponse());

$response = '<WEB><LOANDETAIL><acctrefno>31415</acctrefno><master_acctrefno>0</master_acctrefno><loan_number>TEST100001</loan_number><name>Last Name F Name</name><shortname>Name</shortname><current_payoff_balance>14,358.83</current_payoff_balance><curr_date>07/14/2015</curr_date><curr_maturity_date>07/14/2021</curr_maturity_date><interest_accrued_thru_date>06/06/2017</interest_accrued_thru_date><current_note_amount>10,000.00</current_note_amount><current_principal_balance>10,000.00</current_principal_balance><current_interest_balance>4,358.83</current_interest_balance><current_fees_balance>0.00</current_fees_balance><current_late_charge_balance>0.00</current_late_charge_balance><current_perdiem>6.30</current_perdiem><current_interest_rate>23.00000</current_interest_rate><total_past_due_balance>5,651.05</total_past_due_balance><total_current_due_balance>5,651.05</total_current_due_balance><next_billing_date>06/18/2017</next_billing_date><days_past_due>662</days_past_due><current_pending>0</current_pending><current_impound_balance>0</current_impound_balance><last_payment_date>05/18/2017</last_payment_date><last_payment_amount>5</last_payment_amount><status_code>ACTIVE</status_code><loan_type>Term</loan_type><open_date>07/14/2015</open_date><last_activity_date>05/18/2017</last_activity_date><current_udf1_balance>0.00</current_udf1_balance><current_udf2_balance>0.00</current_udf2_balance><current_udf3_balance>0.00</current_udf3_balance><current_udf4_balance>0.00</current_udf4_balance><current_udf5_balance>0.00</current_udf5_balance><current_udf6_balance>0.00</current_udf6_balance><current_udf7_balance>0.00</current_udf7_balance><current_udf8_balance>0.00</current_udf8_balance><current_udf9_balance>0.00</current_udf9_balance><current_udf10_balance>0.00</current_udf10_balance><current_suspense_balance>0.00</current_suspense_balance><interest_method>SI</interest_method><term_char>Payments</term_char><term>72</term><term_due>72</term_due></LOANDETAIL></WEB>';

function xml2js($xmlnode) {
    $root = (func_num_args() > 1 ? false : true);
    $jsnode = array();

    if (!$root) {
        if (count($xmlnode->attributes()) > 0){
            $jsnode["$"] = array();
            foreach($xmlnode->attributes() as $key => $value)
                $jsnode["$"][$key] = (string)$value;
        }

        $textcontent = trim((string)$xmlnode);
        if (count($textcontent) > 0)
            $jsnode["_"] = $textcontent;

        foreach ($xmlnode->children() as $childxmlnode) {
            $childname = $childxmlnode->getName();
            if (!array_key_exists($childname, $jsnode))
                $jsnode[$childname] = array();
            array_push($jsnode[$childname], xml2js($childxmlnode, true));
        }
        return $jsnode;
    } else {
        $nodename = $xmlnode->getName();
        $jsnode[$nodename] = array();
        array_push($jsnode[$nodename], xml2js($xmlnode, true));
        return json_encode($jsnode);
    }
}


$xml = simplexml_load_string($response);
echo xml2js($xml);

Урожайность:

{
    "WEB": [{
        "_": "",
        "LOANDETAIL": [{
            "_": "",
            "acctrefno": [{
                "_": "31415"
            }],
            "master_acctrefno": [{
                "_": "0"
            }],
            "loan_number": [{
                "_": "TEST100001"
            }],
            "name": [{
                "_": "Last Name F Name"
            }],
            "shortname": [{
                "_": "Name"
            }],
            "current_payoff_balance": [{
                "_": "14,358.83"
            }],
            "curr_date": [{
                "_": "07\/14\/2015"
            }],
            "curr_maturity_date": [{
                "_": "07\/14\/2021"
            }],
            "interest_accrued_thru_date": [{
                "_": "06\/06\/2017"
            }],
            "current_note_amount": [{
                "_": "10,000.00"
            }],
            "current_principal_balance": [{
                "_": "10,000.00"
            }],
            "current_interest_balance": [{
                "_": "4,358.83"
            }],
            "current_fees_balance": [{
                "_": "0.00"
            }],
            "current_late_charge_balance": [{
                "_": "0.00"
            }],
            "current_perdiem": [{
                "_": "6.30"
            }],
            "current_interest_rate": [{
                "_": "23.00000"
            }],
            "total_past_due_balance": [{
                "_": "5,651.05"
            }],
            "total_current_due_balance": [{
                "_": "5,651.05"
            }],
            "next_billing_date": [{
                "_": "06\/18\/2017"
            }],
            "days_past_due": [{
                "_": "662"
            }],
            "current_pending": [{
                "_": "0"
            }],
            "current_impound_balance": [{
                "_": "0"
            }],
            "last_payment_date": [{
                "_": "05\/18\/2017"
            }],
            "last_payment_amount": [{
                "_": "5"
            }],
            "status_code": [{
                "_": "ACTIVE"
            }],
            "loan_type": [{
                "_": "Term"
            }],
            "open_date": [{
                "_": "07\/14\/2015"
            }],
            "last_activity_date": [{
                "_": "05\/18\/2017"
            }],
            "current_udf1_balance": [{
                "_": "0.00"
            }],
            "current_udf2_balance": [{
                "_": "0.00"
            }],
            "current_udf3_balance": [{
                "_": "0.00"
            }],
            "current_udf4_balance": [{
                "_": "0.00"
            }],
            "current_udf5_balance": [{
                "_": "0.00"
            }],
            "current_udf6_balance": [{
                "_": "0.00"
            }],
            "current_udf7_balance": [{
                "_": "0.00"
            }],
            "current_udf8_balance": [{
                "_": "0.00"
            }],
            "current_udf9_balance": [{
                "_": "0.00"
            }],
            "current_udf10_balance": [{
                "_": "0.00"
            }],
            "current_suspense_balance": [{
                "_": "0.00"
            }],
            "interest_method": [{
                "_": "SI"
            }],
            "term_char": [{
                "_": "Payments"
            }],
            "term": [{
                "_": "72"
            }],
            "term_due": [{
                "_": "72"
            }]
        }]
    }]
}
 1
Author: mkaatman, 2017-06-08 04:49:38

Поэтому, используя функцию, найденную здесь , Проанализируйте текст между 2 словами , что казалось обычным способом поиска каждого параметра, который мне важен в ответе, и получите полное значение, не обходя все управляющие символы.

$loanUpdate = $soapClient->__call("METHOD", array($loan_param));

         print "<pre>\n";
         echo "REQUEST:\n" . str_ireplace('><', ">\n<", $soapClient->__getLastRequest()) . "\n"; //Print the Req to check it
         echo "RESPONSE:\n" . str_ireplace('><', ">\n<", $soapClient->__getLastResponse()) . "\n"; //Print the Resp to check it
         print "</pre>\n"; 

         function get_string_between($string, $start, $end){
            $string = ' ' . $string;
            $ini = strpos($string, $start);
            if ($ini == 0) return '';
            $ini += strlen($start);
            $len = strpos($string, $end, $ini) - $ini;
            return substr($string, $ini, $len);
        } //Function for all the info between tags

         //$loanDetail = var_dump($loanUpdate);
         $response = $soapClient->__getLastResponse();
         $xml = new SimpleXMLElement($response);
         $body = $xml->xpath('//SBody');
         $haystack = str_ireplace('><', ">\n<", $soapClient->__getLastResponse());



        $parsed = get_string_between($haystack, '&lt;acctrefno&gt;', '&lt;/acctrefno&gt;');

        if(is_string($haystack)){
            //echo $haystack;
            echo 'LoanNumber should be here: ' . $parsed; 
        }else {
            echo 'its not a string';
        } 




    } catch (SoapFault $fault) {
         $error = 1;
    }
 1
Author: Allen Craig, 2017-08-01 21:32:24