simplexml获取一个值并分配给php变量

时间:2016-04-25 23:23:19

标签: php xml simplexml

我正在尝试获取xml响应并在响应中提取一个变量并设置为php变量以用于购物车。我有卷曲响应,并使用simplexml。我已经尝试了几种方法将它回显到浏览器并且它们都是空的?

以下是回复和我尝试的内容。

         $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;

这是响应转储

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
                                            )

                                    )

                            )

                    )

            )

    )

1 个答案:

答案 0 :(得分:0)

XML标记名称区分大小写。

因此,当您导航到特定节点时,请确保也使用正确的标记。

$parser->GetRateQuoteResponse->GetRateQuoteResult->Result->Origin->City;

$parser->GetRateQuoteResponse->GetRateQuoteResult->Result->Charges
       ->Charge[6]->A‌​mount;
相关问题