Parsing XML with PHP - php

I know taht there already are many question about this subject but I haven't find the solution for my problem.
<?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:Header>
<PeopleCountingServiceHeader xmlns="http://localhost/countingws/">
<Username>username</Username>
<Password>password</Password>
<Camera>essai</Camera>
</PeopleCountingServiceHeader>
</soap:Header>
<soap:Body>
<InsertData xmlns="http://ai-sense.com/counting/">
<impacts>
<Impact>
<TimeStamp>2014-10-23T15:17:51.879</TimeStamp>
<Type>Output</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:17:52.753</TimeStamp>
<Type>Input</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:17:54.375</TimeStamp>
<Type>Output</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:17:55.186</TimeStamp>
<Type>Input</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:18:28.897</TimeStamp>
<Type>Output</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:18:29.771</TimeStamp>
<Type>Input</Type>
<Layer>0</Layer>
</Impact>
<Impact>
<TimeStamp>2014-10-23T15:22:41.219</TimeStamp>
<Type>Output</Type>
<Layer>0</Layer>
</Impact>
</impacts>
</InsertData>
</soap:Body>
</soap:Envelope>
And I want to retrieve for example the username.
I have already try somethinf like this :
$request =simplexml_load_file("php://input", NULL, TRUE);
echo "request: "$request->{"soap:Header"}->PeopleCountingServiceHeader->Username;
How can I access to my datas ?

This is not just XML, but SOAP. PHP has a specific extension for it: ext/soap.
SOAP is an XML format to transfer objects between systems. You could parse it manually, but a specific SOAP implementation is a lot more convenient.
If you parse in manually you have to consider the namespaces. Here are 3 namespaces in your example XML:
http://schemas.xmlsoap.org/soap/envelope/ - the SOAP envelope, using a soap prefix/alias
http://localhost/countingws/ - elements inside soap:Header, no prefix/alias
http://ai-sense.com/counting/ - elements inside soap:Body, no prefix/alias

Related

Sending soap request with attributes using PHP SoapClient

I`m having remote wsdl API that requires some xml attributes in request.
Integration of this API must be made usind PHP (and possibly SoapClient).
I`m creating SimpleXMLElement() object like
<?xml version="1.0" encoding="UTF-8"?>
<Body>
<FlightMatrixRequest xmlns="http://www.somesite.com/webservices">
<flightMatrixRQ Target="Test" Version="1.0">
<AirItinerary DirectionInd="OneWay">
<OriginDestinationOptions>
<OriginDestinationOption>
<FlightSegment DepartureDateTime="2022-05-30T00:00:00+11:00"
ArrivalDateTime="2022-05-30T00:00:00+11:00" RPH="1">
<DepartureAirport LocationCode="BKK"/>
<ArrivalAirport LocationCode="CNX"/>
<MarketingAirline Code="SL"/>
</FlightSegment>
</OriginDestinationOption>
</OriginDestinationOptions>
</AirItinerary>
<TravelerInfoSummary>
<AirTravelerAvail>
<AirTraveler>
<PassengerTypeQuantity Code="ADT" Quantity="1"/>
</AirTraveler>
</AirTravelerAvail>
<AirTravelerAvail>
<AirTraveler>
<PassengerTypeQuantity Code="CNN" Quantity="1"/>
</AirTraveler>
</AirTravelerAvail>
<AirTravelerAvail>
<AirTraveler>
<PassengerTypeQuantity Code="INF" Quantity="1"/>
</AirTraveler>
</AirTravelerAvail>
</TravelerInfoSummary>
</flightMatrixRQ>
</FlightMatrixRequest>
</Body>
Converting it to array using
$array = json_decode(json_encode($xml), true);
Then requesting
$soapClient->__soapCall($method, $array)
And getting errors from API
$soapClient->__getLastRequest() returns
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.somesite.com/webservices"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<env:Body>
<ns1:FlightMatrixRequest>
<ns1:flightMatrixRQ>
<ns1:AirItinerary>
<ns1:OriginDestinationOptions>
<ns1:OriginDestinationOption>
<ns1:FlightSegment>
<ns1:DepartureAirport/>
<ns1:ArrivalAirport/>
<ns1:MarketingAirline/>
</ns1:FlightSegment>
</ns1:OriginDestinationOption>
</ns1:OriginDestinationOptions>
</ns1:AirItinerary>
<ns1:TravelerInfoSummary>
<ns1:AirTravelerAvail>
<ns1:AirTraveler>
<ns1:PassengerTypeQuantity/>
<ns1:PassengerIdentificationDocument xsi:nil="true"/>
</ns1:AirTraveler>
</ns1:AirTravelerAvail>
<ns1:AirTravelerAvail>
<ns1:AirTraveler>
<ns1:PassengerTypeQuantity/>
<ns1:PassengerIdentificationDocument xsi:nil="true"/>
</ns1:AirTraveler>
</ns1:AirTravelerAvail>
<ns1:AirTravelerAvail>
<ns1:AirTraveler>
<ns1:PassengerTypeQuantity/>
<ns1:PassengerIdentificationDocument xsi:nil="true"/>
</ns1:AirTraveler>
</ns1:AirTravelerAvail>
</ns1:TravelerInfoSummary>
</ns1:flightMatrixRQ>
</ns1:FlightMatrixRequest>
</env:Body>
</env:Envelope>
As you may see there is no attribute`s data, like DepartureDateTime/LocationCode etc.
Could not find any solution for this case. Possible it may be fixed by using regular curl request with prepared body/headers but it does not look like a good idea.
Any ideas?

simplexml_load_string asXML not empty, but children() function returns empty? [duplicate]

This question already has answers here:
SimpleXMLElement Access elements with namespace?
(4 answers)
Closed 1 year ago.
echo $xml->asXML();
Prints the following, and I am tring to access to elements here like: InvoiceStateResult
<?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"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsa:Action>http://tempuri.org/SendEArchiveDataResponse</wsa:Action>
<wsa:MessageID>urn:uuid:72e8aaf0-b36d-422f-ab0b-486c17c50c83</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:fc0a3e9d-40c1-4f3b-9517-8002825b7217</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-3a82271a-a910-4062-81aa-984468387047">
<wsu:Created>2021-05-28T12:12:23Z</wsu:Created>
<wsu:Expires>2021-05-28T12:27:23Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<SendEArchiveDataResponse
xmlns="http://tempuri.org/">
<SendEArchiveDataResult>
<Invoices>
<InvoiceStateResult>
<ServiceResult>Error</ServiceResult>
<UUID>11111111-2222-3333-4444-555555555555</UUID>
<InvoiceId>T612014000000053</InvoiceId>
<StatusDescription>INVOICE EXISTS</StatusDescription>
<StatusCode>29</StatusCode>
<ErrorCode>0</ErrorCode>
<ReferenceNo>T612014000000053</ReferenceNo>
</InvoiceStateResult>
</Invoices>
<ServiceResult>Error</ServiceResult>
<ServiceResultDescription>This invoice processed before InvoiceId : TRL2021000000019 , UUID : DB3642EB-7A5F-40FD-8DF8-A922CA113837 SenderTaxID : 3324502175 . </ServiceResultDescription>
<Source>IntegrationWebService</Source>
<ErrorCode>30</ErrorCode>
<invoiceCount>1</invoiceCount>
</SendEArchiveDataResult>
</SendEArchiveDataResponse>
</soap:Body>
</soap:Envelope>
However I couldn't reach any of the nodes. I tried this:
foreach($xml->children() as $child) {
echo "Child node: " . $child . "</br>";
}
and it returns empty.
How will I access the nodes ?
Thanks
Simple XML, is - as the name suggests, a very simple implementation and it looks for standard namespaces. You can use registerXPathNamespace to look for non custom ones. See example below that works for your code.
$xml = simplexml_load_string($string);
$xml->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
print_r($xml->xpath('//soap:Body')[0]->SendEArchiveDataResponse->SendEArchiveDataResult->Invoices->InvoiceStateResult);

PHP - SOAP request - nest xml as param?

I need to make the structure of my XML SOAP request to look like following:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.fines.pl/api/contract">
<SOAP-ENV:Body>
<ns1:newApplicationRequest>
<user_login>XYZ</user_login>
<user_password>XYZ</user_password>
<contract>
<?xml version="1.0" encoding="UTF-8"?>
<sof:Contract xmlns:s="http://www.fines.pl/simple" xmlns:sof="http://www.fines.pl/sof" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fines.pl/sof model.xsd ">
<product>
<prefix>MOP</prefix>
</product>
<participants>
<customers>
<main_borrower>
<personal_data>
<pesel>85050949761</pesel>
<firstname>Anna</firstname>
<lastname>Test</lastname>
<firstname_father></firstname_father>
<firstname_mother></firstname_mother>
<secondname />
<sex>female</sex>
</personal_data>
<contact_data>
<addresses>
<address>
<type>registered</type>
<street_name>Grunwaldzka</street_name>
<block_number>11</block_number>
<flat_number>5</flat_number>
<postal_code>80-100</postal_code>
<city>GdaƄsk</city>
</address>
</addresses>
<phones_mobile>
<phone_mobile>
<type>personal</type>
<number>602200300</number>
</phone_mobile>
</phones_mobile>
</contact_data>
<incomes>
<income>
<type>employment</type>
<main_income>true</main_income>
<fixed_term_contract>false</fixed_term_contract>
<paychecks>
<paycheck>
<amount_net>
<amount>1444.00</amount>
<currency>PLN</currency>
</amount_net>
<type>base</type>
</paycheck>
</paychecks>
</income>
</incomes>
<household_pointer>/households.0</household_pointer>
</main_borrower>
</customers>
</participants>
</sof:Contract></contract></ns1:newApplicationRequest></SOAP-ENV:Body></SOAP-ENV:Envelope>
I've set my $params variable to contain user_login and user_password elements with their respective values, but I have no idea how may I set this contract param with required content.
Providing $contract variable with XML code (which i thought may be a workaround) poop the Fatal error: Uncaught SoapFault exception: [xml_structure] String could not be parsed as XML error.
Would be grateful for explaining how could this be done.
You have declared <?xml?> twice , one on the first line and second on the eight. That's the only error I see in the xml side of things.

SOAP response to PHP Array, reaching node Attributes

I'm trying to convert the following response into an array in PHP, I want to reach the data inside 'Timbre', I need the values of:
UUID
FechaTimbrado
SelloSAT
The solutions I've tried around here convert the nodes ok, but I can't find a way to reach nothing beyond 'Timbre', hope you can help me find a way to do this, here's the response I'm receiving:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<ResponseHeader trackId="119588" RequestDateTime="2017-11-09T11:55:39.0205969-06:00" IPCaller="187.190.165.93" ClienteId="0" Estatus="1" ID="91a407fe-c1c2-40af-bc2d-5aee58606161" Detail="" Ambiente="DESARROLLO-01" xmlns="https://cfdi.timbrado.com.mx/timbradov2">
<CFDI Id="CFDI33" FechaHora="2017-11-09T11:45:30" RFCEmisor="XXXHHK2343434" RFCReceptor="GFGDS45&667" Serie="G" Folio="35010" />
</ResponseHeader>
</soap:Header>
<soap:Body>
<GeneraTimbreResponse xmlns="https://cfdi.timbrado.com.mx/timbradov2">
<GeneraTimbreResult>
<Timbre>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd" Version="1.1" UUID="91A407FE-C1C2-40AF-BC2D-5AEE58606161" FechaTimbrado="2017-11-09T11:55:39" RfcProvCertif="ASE0209252Q1" Leyenda="Timbre de prueba" SelloCFD="WCldH0Oan7h2VK3MuMM3Nv8HAg/OlCaKF2VLz/1u81yvZItgZuYia1Aaz27hOYsqBZ/m5/OhAaL6TZ43MZSF6zK9tZz+Fgu9wS2QJ+ubk/83oIjQDwLHCuJL/l5VRZI29RoPUfs3VDb+tD9mqeEinQwfLJG1YkoCIWC/4oXxL1oX5DEXqMW/sK52jpJh7exyqw+GzBA+LUBaSi+aGHlown8cEQmZOZieeUW5uXjDPQBdJBZ/XkRgfqzAiS/Tfj9a61B0Nrm7JyejagPZVW7E3gbcmFunWTbGwNEYvqWkgtnO7aoTzrVcjK3YX4t2zKPt8F7BGq+MSL/fge174IttGQ==" NoCertificadoSAT="20001000000300022323" SelloSAT="NhzPGgUh+1vQJkidObRpp+IgjSdOm6wwgMVVcPdEVmVEbFgCBW42a7grzg8toe9d/ZlHhka5g6h63E5jp3xKcq2KnWw+dQbrbh8xDNHC+7tPXEJN9T5JU8ZifpvTIu9g89TYERUs/4sLgAYtqAmV9AIQK17LoAIiNRdUzwWiiHUCLaqL7k0bmJYvfeuYAnkw52LfyxzLDg6TyodT/1LjJVwhGLyaOcOwCDObHH8ukZj0d2aOwOEE0IJBbbIgh3VaIp0/EvvFn1I1F9BnrLHVZFEcR3ZeP/TyLJmuOtxLGDnPtnaVQTrt0sQb43bG2R2ut5Bt3uS12xOMx3IfDr/3FQ==" />
</Timbre>
</GeneraTimbreResult>
</GeneraTimbreResponse>
</soap:Body>
</soap:Envelope>
So I solved it like this, if anyone knows a 'classier' way , let me know
//here I read the result string I posted above
$soap = simplexml_load_string(html_entity_decode($this->resultado_timbre));
//I read the nodes until I reach the 'Timbre' one
$response = $soap->children('http://www.w3.org/2003/05/soap-envelope')->Body->children()->children()->children()->Timbre->children('tfd',TRUE);
//The attributes are readable in a nice array
$atributos=$response->attributes();
Hope it helps someone!

Parsing SOAP response with PHP

SO,
I'm receiving the following SOAP response as a string:
<?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>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<rs:data ItemCount="3">
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="2;#2016-10-08 17:29:29" ows_ID="2" ows_Created_x0020_Date="2;#2016-10-08 17:27:40" ows_FileLeafRef="2;#18380014229851.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_2.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asfd" ows_Last_x0020_Modified="3;#2016-10-08 17:29:29" ows_ID="3" ows_Created_x0020_Date="3;#2016-10-08 17:27:41" ows_FileLeafRef="3;#18380014229851_2.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_3.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="4;#2016-10-08 17:30:03" ows_ID="4" ows_Created_x0020_Date="4;#2016-10-08 17:27:41" ows_FileLeafRef="4;#18380014229851_3.jpg" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I am attempting to get each of the "z:row" entries, but am struggling due to the namespaces (after some googling that's what i'm understanding them to be called).
Here is the code I am using:
$xml = simplexml_load_string($sp->soapClient->__last_response);
foreach($xml->GetListItemsResult as $item)
{
$ns_li = $item->children('uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882');
$ns_rs = $ns_li->children('urn:schemas-microsoft-com:rowset');
$ns_z = $nr_rs->children('#RowsetSchema');
echo $ns_z->row;
}
Right now I am getting no output from echo statement. What am I doing wrong?
You can use XPath expression to get specific part of XML document using various criteria. For example, to get z:row elements anywhere in the XML document you can simply do //z:row, after registering the prefix z :
$xml->registerXPathNamespace("z", "#RowsetSchema");
foreach($xml->xpath('//z:row') as $item)
{
echo $item["ows_LinkFilename"] ."\n";
}
eval.in demo
output :
18380014229851.jpg
18380014229851_2.jpg
18380014229851_3.jpg

Categories