1
votes

I currently have a script parsing data from a Web Service, all examples I've found so far tell me the result is just to be parsed as XML, but without the result actually being valid XML this is a bit difficulty.


My XML Request sent to the Web Service:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0"> 
   <soapenv:Header/> 
   <soapenv:Body> 
      <ns:ShippingGuideRequest> 
         <ns:UserInformation> 
            <ns:Usercode>ae34ab27-2023-41f5-b800-013f0d0a6015</ns:Usercode> 
         </ns:UserInformation> 
         <ns:ProductIds> 
            <ns:ProductId>SERVICEPAKKE</ns:ProductId> 
         </ns:ProductIds> 
         <ns:Packages> 
        <ns:Package packageId="7,"> 
               <ns:GrossWeight unitCode="GRM">500</ns:GrossWeight> 
               <ns:FromPostalCode>4045</ns:FromPostalCode> 
               <ns:ToPostalCode>4045</ns:ToPostalCode> 
            </ns:Package> 
         </ns:Packages> 
      </ns:ShippingGuideRequest> 
   </soapenv:Body> 
</soapenv:Envelope>

I am NOT able to use the built in SoapClient that comes with PHP5 and above, as I do not approve of forcing my users to have this configured as I've experienced many of my test cases to not have. I have tried doing the request using plain sockets as well as cURL, but this did not yield any different results.


The result I get from the call is as follows;

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:ShippingGuideResponse xmlns:ns="http://www.bring.no/logistics/shippingguide/1.0"><ns:Packages><ns:Package packageId="7,"><ns:Product><ns:ProductId>SERVICEPAKKE</ns:ProductId><ns:GuiInformation><ns:SortOrder>11</ns:SortOrder><ns:MainDisplayCategory>Hent sendingen selv</ns:MainDisplayCategory><ns:SubDisplayCategory/><ns:DisplayName>På postkontor eller post i butikk</ns:DisplayName><ns:ProductName>Klimanøytral Servicepakke</ns:ProductName><ns:DescriptionText>Madla. Åpningstider Man - Fre: 1000-1800, Lør: 1000-1500</ns:DescriptionText><ns:HelpText>Sendingen er en Klimanøytral Servicepakke som blir levert til mottakers postkontor/ post i butikk. Mottaker kan velge å hente sendingen på et annet postkontor/post i butikk enn sitt lokale. Mottaker varsles om at sendingen er ankommet via SMS, e-post eller hentemelding i postkassen. Transporttid er normalt 1-3 virkedager, avhengig av strekning. Sendingen kan spores ved hjelp av sporingsnummeret.</ns:HelpText><ns:Tip/></ns:GuiInformation><ns:Price currencyIdentificationCode="NOK"><ns:PackagePriceWithoutAdditionalServices><ns:AmountWithoutVAT>66.00</ns:AmountWithoutVAT><ns:VAT>16.50</ns:VAT><ns:AmountWithVAT>82.50</ns:AmountWithVAT></ns:PackagePriceWithoutAdditionalServices></ns:Price><ns:ExpectedDelivery><ns:WorkingDays>1</ns:WorkingDays><ns:UserMessage/><ns:AlternativeDeliveryDates/></ns:ExpectedDelivery></ns:Product></ns:Package></ns:Packages><ns:TraceMessages><ns:Message>You are using an outdated schema version (1). Most recent schema version is 4.</ns:Message></ns:TraceMessages></ns:ShippingGuideResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

As you can see the XML request is lacking the XML start tags, and as such SimpleXMLElement and similar in PHP is unable to interpret it.

Finally, the code I use to send my request, hopefully it's something trivial like a header I'm missing or similar.

$ch = curl_init("http://fraktguide.bring.no/fraktguide/ws/1.0/");

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: ' . strlen($xml) ));

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_PORT, 80);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

$result = curl_exec($ch);

1

1 Answers

0
votes

The data was fetched, but reading it without a reference point seemed to be an issue.

The solution was traversing it using XPath.