1

I am trying to parse the namespace Rate under the QuoteDetail section. Here is my response from the web service. Any help on getting that Rate node extracted would be awesome.

<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:Body>
<RateQuoteByAccountResponse xmlns="https://webservices.rrts.com/ratequote/">
<RateQuoteByAccountResult>
<QuoteNumber>2066833</QuoteNumber>
<NetCharge>676.75</NetCharge>
<Customer>
<AccountNumber>*******</AccountNumber>
<Name>*****</Name>
<Address1>**</Address1>
<Address2>**</Address2>
<City>***</City>
<State>**</State>
<ZipCode>*****</ZipCode>
</Customer>
<RoutingInfo>
<DestinationState>CA</DestinationState>
<DestinationZip>90210</DestinationZip>
<OriginState>NC</OriginState>
<OriginZip>27360</OriginZip>
<EstimatedTransitDays>5</EstimatedTransitDays>
<OriginTerminal>Charlotte</OriginTerminal>
</RoutingInfo>
<RateDetails>
<QuoteDetail>
<ActualClass>60</ActualClass>
<RatedClass>60</RatedClass>
<Charge>533.45</Charge>
<Code></Code>
<Description></Description>
<Rate>106.69</Rate>
<Weight>500</Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>41.95</Charge>
<Code>ID</Code>
<Description>Inside Delivery</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>32</Charge>
<Code>CFP</Code>
<Description>Prepaid COD Fee</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>69.35</Charge>
<Code>FSC</Code>
<Description>Fuel Surcharge - 13.00 %</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
</RateDetails>
<OriginType>O</OriginType>
<PaymentType>P</PaymentType>
<CODAmount>0</CODAmount>
<ShipmentDate>2105-08-07T00:00:00</ShipmentDate>
<CustomerCubicFoot>0</CustomerCubicFoot>
<HawaiianRatedCubicFoot>0</HawaiianRatedCubicFoot>
</RateQuoteByAccountResult>
</RateQuoteByAccountResponse>
</soap:Body>
</soap:Envelope>

This is the code I am using to try and parse but getting error: Call to a member function children() on string

$xml = $curl->response;
$rate = (string)$xml->children('soap', true)->Body->RateQuoteByAccountResponse->RateQuoteByAccountResult->RateDetails->QuoteDetail->Rate;
Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
  • `$xml = $curl->response;` / `$rate = (string)$xml->children('soap', true)-> [...] ` ---- `$curl->response` is of type string, you can not call methods on strings (unless you install a poc extension for that). A programming question is not determined on what you would like to do (in the end), but about a concrete programming question. What is your programming question? How is it different from existing material here on site? – hakre Aug 10 '15 at 15:46
  • You're perhaps looking for: [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/q/3577641/367456). – hakre Aug 10 '15 at 15:49

1 Answers1

0

Try wrapping xml string around as follow:

<?php
$string = '<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:Body>
<RateQuoteByAccountResponse xmlns="https://webservices.rrts.com/ratequote/">
<RateQuoteByAccountResult>
<QuoteNumber>2066833</QuoteNumber>
<NetCharge>676.75</NetCharge>
<Customer>
<AccountNumber>*******</AccountNumber>
<Name>*****</Name>
<Address1>**</Address1>
<Address2>**</Address2>
<City>***</City>
<State>**</State>
<ZipCode>*****</ZipCode>
</Customer>
<RoutingInfo>
<DestinationState>CA</DestinationState>
<DestinationZip>90210</DestinationZip>
<OriginState>NC</OriginState>
<OriginZip>27360</OriginZip>
<EstimatedTransitDays>5</EstimatedTransitDays>
<OriginTerminal>Charlotte</OriginTerminal>
</RoutingInfo>
<RateDetails>
<QuoteDetail>
<ActualClass>60</ActualClass>
<RatedClass>60</RatedClass>
<Charge>533.45</Charge>
<Code></Code>
<Description></Description>
<Rate>106.69</Rate>
<Weight>500</Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>41.95</Charge>
<Code>ID</Code>
<Description>Inside Delivery</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>32</Charge>
<Code>CFP</Code>
<Description>Prepaid COD Fee</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>69.35</Charge>
<Code>FSC</Code>
<Description>Fuel Surcharge - 13.00 %</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
</RateDetails>
<OriginType>O</OriginType>
<PaymentType>P</PaymentType>
<CODAmount>0</CODAmount>
<ShipmentDate>2105-08-07T00:00:00</ShipmentDate>
<CustomerCubicFoot>0</CustomerCubicFoot>
<HawaiianRatedCubicFoot>0</HawaiianRatedCubicFoot>
</RateQuoteByAccountResult>
</RateQuoteByAccountResponse>
</soap:Body>
</soap:Envelope>';

$string = <<<XML
$string
XML;

$XmlArray = new SimpleXMLElement($string);
echo $ErrorCode = $XmlArray->children("soap", true)->Body->
                    children()->RateQuoteByAccountResponse->
                    children()->RateQuoteByAccountResult->children()->RateDetails->children()->QuoteDetail->children()->Rate;
?>

You'll get first Rate - 106.69 from the list.

Kamal Joshi
  • 1,298
  • 3
  • 25
  • 45
  • i get this error when using your response Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /Applications/MAMP/htdocs/rrts.php:98 Stack trace: #0 /Applications/MAMP/htdocs/rrts.php(98): SimpleXMLElement->__construct('') #1 {main} thrown in /Applications/MAMP/htdocs/rrts.php on line 98 – Chris Kennedy Aug 10 '15 at 16:39
  • tweaked your response just a bit and got it. Thanks for pointing me in the right direction!!!! – Chris Kennedy Aug 10 '15 at 16:44
  • Glad to know, cheers!! – Kamal Joshi Aug 11 '15 at 06:52