2016-04-25 8 views
0

Ich versuche, eine XML-Antwort zu erhalten und eine Variable in der Antwort zu extrahieren und auf eine PHP-Variable für die Verwendung in einem Einkaufswagen zu setzen. Ich habe die Curl-Antwort und verwende Simplexml. Ich habe mehrere Möglichkeiten ausprobiert, um es auf den Browser zu übertragen und sie sind alle leer?simplexx einen Wert erhalten und der PHP-Variablen zuweisen

Unten ist die Antwort und was ich versucht habe.

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

Hier ist die Antwort 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 
              ) 

            ) 

          ) 

        ) 

      ) 

    ) 

)

+1

'echo (string) $ parser-> GetRateQuoteResponse-> GetRateQuoteResult-> result-> Charges-> Charge [6] -> Betrag;' ' –

+1

Wenn Sie XML-Quelle veröffentlichen kann es helfen. BTW entfernen Sie alle Ihre (String) Casting. Sie sind nutzlos in 'simplexml_load_string' und' echo'. Außerdem ist die 'echo'-Syntax falsch (Beispiel ist richtig). Sie müssen bei der Zuweisung in eine Zeichenfolge umwandeln, wobei "echo" direkt als Zeichenfolge für Sie interpretiert wird. – fusion3k

+0

Ich muss etwas falsch machen? Ich habe das oben Gesagte versucht und sogar ein paar Kombinationen ausprobiert, um irgendetwas zu wiederholen und sie sind alle leer? 'code' echo (string) $ parser-> GetRateQuoteResponse-> GetRateQuoteResult-> result-> origin-> city; Sollte ARCHBOLD zurückkehren Nichts wird Echo? –

Antwort

0

XML-Tag-Namen sind Case Sensitive.

Wenn Sie also zu einem bestimmten Knoten navigieren, stellen Sie sicher, dass Sie auch die richtigen Tags verwenden.

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

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

Hätte das gesehen! Vielen Dank –

Verwandte Themen