I am trying to get an xml response and extract one variable in the response and set to a php variable for use in a shopping cart. I have the curl response and am using simplexml. I have tried several ways to echo it to the browser and they are all empty?
Below is the response and what I have tried.
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
$parser = simplexml_load_string((string)$response2);
echo $parser->(string)GetRateQuoteResponse->GetRateQuoteResult->result->Charges->charge[6]->amount;
Here is the response dump
SimpleXMLElement Object
(
[GetRateQuoteResponse] => SimpleXMLElement Object
(
[GetRateQuoteResult] => SimpleXMLElement Object
(
[WasSuccess] => true
[Messages] => SimpleXMLElement Object
(
)
[Result] => SimpleXMLElement Object
(
[Origin] => SimpleXMLElement Object
(
[City] => ARCHBALD
[StateOrProvince] => PA
[ZipOrPostalCode] => 18403
[CountryCode] => USA
)
[Destination] => SimpleXMLElement Object
(
[City] => DUNEDIN
[StateOrProvince] => FL
[ZipOrPostalCode] => 34698
[CountryCode] => USA
)
[OriginServiceCenter] => SimpleXMLElement Object
(
[Code] => SCR
[Location] => Scranton, PA
[ZipCode] => 18640-9640
[Phone] => 1-800-654-6927
[Address1] => 115 Armstrong Road
[Address2] => SimpleXMLElement Object
(
)
[City] => Pittston Township
[State] => PA
)
[DestinationServiceCenter] => SimpleXMLElement Object
(
[Code] => LKD
[Location] => Lakeland, FL
[ZipCode] => 33809-1720
[Phone] => 1-800-940-7811
[Address1] => 8425 Epicenter Blvd.
[Address2] => SimpleXMLElement Object
(
)
[City] => Lakeland
[State] => FL
)
[CustomerDiscounts] => $490.88
[Charges] => SimpleXMLElement Object
(
[Charge] => Array
(
[0] => SimpleXMLElement Object
(
[Type] => SimpleXMLElement Object
(
)
[Title] => Class: 55
[Weight] => 280
[Rate] => $224.76
[Amount] => $629.33
)
[1] => SimpleXMLElement Object
(
[Type] => GROSS
[Title] => Gross Charge
[Weight] => 280
[Rate] => SimpleXMLElement Object
(
)
[Amount] => $629.33
)
[2] => SimpleXMLElement Object
(
[Type] => DISCNT
[Title] => R+L Discount Saves This Much
[Weight] => SimpleXMLElement Object
(
)
[Rate] => 78%
[Amount] => $490.88
)
[3] => SimpleXMLElement Object
(
[Type] => DISCNF
[Title] => Discounted Freight Charge
[Weight] => SimpleXMLElement Object
(
)
[Rate] => SimpleXMLElement Object
(
)
[Amount] => $138.45
)
[4] => SimpleXMLElement Object
(
[Type] => FUEL
[Title] => Fuel Surcharge
[Weight] => SimpleXMLElement Object
(
)
[Rate] => 15.5%
[Amount] => $21.46
)
[5] => SimpleXMLElement Object
(
[Type] => RC
[Title] => Residential Delivery Fee
[Weight] => SimpleXMLElement Object
(
)
[Rate] => $26.50
[Amount] => $26.50
)
[6] => SimpleXMLElement Object
(
[Type] => NET
[Title] => Net Charge
[Weight] => SimpleXMLElement Object
(
)
[Rate] => SimpleXMLElement Object
(
)
**[Amount] => $186.41**
)
)
)
[Messages] => SimpleXMLElement Object
(
[Message] => Array
(
[0] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => * Not to Exceed 20 Feet or 20,000 Lbs.
)
[1] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => Note: Some residential deliveries may require additional days of service.
)
[2] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => * This quote is based on information you provide. The actual charges shall be determined by actual shipment characteristics, and any accessorial charges that are applicable to the shipment at time of shipment.
)
[3] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => For classes higher than 300, contact the Rate department at 800-535-1983.
)
[4] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => Please note that a sufferance and in-bond storage charges accumulated in the customs process are the responsibility of the debtor of the freight charges.
)
[5] => SimpleXMLElement Object
(
[Title] => SimpleXMLElement Object
(
)
[Text] => This rate is not applicable to tradeshows. Please contact our Rate Specialist at 800-535-1983 for more information.
)
)
)
[ServiceLevels] => SimpleXMLElement Object
(
[ServiceLevel] => Array
(
[0] => SimpleXMLElement Object
(
[Title] => Standard Service
[Code] => STD
[QuoteNumber] => 14385117
[ServiceDays] => 2
[Charge] => $655.83
[NetCharge] => $186.41
)
[1] => SimpleXMLElement Object
(
[Title] => Guaranteed Service
[Code] => GSDS
[QuoteNumber] => 28585290
[ServiceDays] => 2
[Charge] => $35.00
[NetCharge] => $221.41
)
[2] => SimpleXMLElement Object
(
[Title] => Guaranteed AM Service
[Code] => GSAM
[QuoteNumber] => 38230526
[ServiceDays] => 2
[Charge] => $70.00
[NetCharge] => $256.41
)
)
)
)
)
)
)
XML Tag Names Are Case Sensitive.
So when you navigate to a specific node, make sure to use the right tags as well.
$parser->GetRateQuoteResponse->GetRateQuoteResult->Result->Origin->City;
$parser->GetRateQuoteResponse->GetRateQuoteResult->Result->Charges
->Charge[6]->Amount;