2016-04-25 13 views
0

xmlレスポンスを取得し、レスポンスに1つの変数を抽出し、ショッピングカートで使用するためにphp変数に設定しようとしています。私はカールの反応があり、simplexmlを使用しています。私はそれをブラウザにエコーするいくつかの方法を試しましたが、それらはすべて空ですか?simplexmlが1つの値を取得して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-> Charges->チャージ[6] - >量;' ' –

+1

あなたはそれが助けることができるXMLソースを投稿した場合。あなたの '(文字列)'キャストをすべて削除してください。 'simplexml_load_string'と' echo'では無用です。さらに、 'echo'構文が間違っています(マークの例が正しい)。あなたは代入時に文字列にキャストしなければなりません。 'echo'文字列は直接文字列としてキャストしなければなりません。 – fusion3k

+0

私は何か間違っていなければなりませんか?私は上記を試して何かをエコーするためにいくつかの組み合わせを試してみても、それらはすべて空ですか? 'code' echo(文字列)$ 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

それを見たことがありますか?ありがとう –

関連する問題