Good time of day! I've been fighting for a week now, I can't parse an XML of the form:

<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Header xmlns:ser="http://www.a-3.ru/partners/services/"/> <soapenv:Body xmlns:ser="http://www.a-3.ru/partners/services/"> <ser:getPenaltiesResponse> <result> <resCode>0</resCode> </result> <penalties> <penalty billNumber="18810152160623063633" billDate="2016-06-23" validUntil="2016-08-23" amount="500" addInfo="ШТРАФ ПО АДМИНИСТРАТИВНОМУ ПРАВОНАРУШЕНИЮ ПОСТАНОВЛЕНИЕ №18810152160623063633;Подразделение: ЦАФАП ОДД ГИБДД ГУ МВД России по Нижегородской области;Получатель: УФК по Нижегородской области (ГУ МВД России по Нижегородской области);Банк Получателя: Волго-Вятское ГУ Банка России;Номер счёта Получателя: 40101810400000010002;ИНН: 5260040766;КПП: 526001001;БИК Банка Получателя: 042202001;ОКАТО: 22701000;КБК: 18811630020016000140;" regCert="52СМ499648" docName="regCert" docNumber="52СМ499648" payStatus="0" quittance="3"/> </penalties> </ser:getPenaltiesResponse> </soapenv:Body> </soapenv:Envelope> 

I load the response from the server: $result = simplexml_load_string($result); Further I sort it out in the following way, since there is a namespace:

 $namespaces = $result->getNamespaces(true); $ser = $result->children($namespaces['ser']); $body = $ser->getPenaltiesResponse; 

And then I just don’t understand how to get to resCode or penalty . Please help me figure it out!

    1 answer 1

    To begin with, the counter-question: why are you obviously trying to parse a SOAP chunk manually? PHP has a regular client for the entire protocol.

    Well, if this is not SOAP, despite the characteristic namespace, then you can reach it like this:

     $result = simplexml_load_string($xml); $response = $result->children('soapenv', true) ->Body ->children('ser', true) ->getPenaltiesResponse ->children(null); var_dump((string) $response->result->resCode); var_dump((string) $response->penalties->penalty->attributes()->billNumber); 
    • Thanks a lot, helped. Yes, there is not clear what kind of SOAP hybrid is used, here we have to deal with parsing. Thank you so much again. - Anton Chaplygin
    • And you will not tell $response->penalties how to determine the existence of $response->penalties , it does not always just exist. I tried isset for the attribute $response->penalties->penalty->attributes()->billNumber does not work. - Anton Chaplygin
    • That's why I do not like simplexml, it works unnecessarily helpfully and as a result it is not clear, but what is there in general in the document and what structure. Try $response->penalties->count() . If there is no element, then it will be 0. - Shallow
    • Thank you very much! Bail out! - Anton Chaplygin
    • Hello, I already tortured you, but help parse the xml of one more type, we have only two of them, I’m doing your principle but it’s impossible, I can’t get to resCode and resMessage - Anton Chaplygin