2016-04-25 71 views
0

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

下面是答复和我试过的。

  $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

'回声(字符串)$ parser-> GetRateQuoteResponse-> GetRateQuoteResult-> result->押记─>费[6] - >金额;'' –

+1

如果您发布的XML源,它可以帮助。顺便说一句,删除所有的'(串)'铸造。它们在'simplexml_load_string'和'echo'中没有用处。另外''echo'语法是错误的(标记示例是正确的)。你必须在分配时转换为字符串,'echo'作为字符串直接转换为你。 – fusion3k

+0

我一定是做错了什么?我尝试了上述方法,甚至尝试了几种组合来回应任何事情,而且都是空的? 'code' echo(string)$ parser-> GetRateQuoteResponse-> GetRateQuoteResult-> result-> origin-> city; 应该返回ARCHBOLD什么都不会回应? –

回答

0

XML标签名称是区分大小写。

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

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

$parser->GetRateQuoteResponse->GetRateQuoteResult->Result->Charges 
     ->Charge[6]->A‌​mount; 
+1

应该看到了!谢谢 –