转换为对象的Access XML响应

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (63)

是否有人知道如何访问[@attributes]SOAPAPI返回的SimpleXMLElement对象中的数组?

我无法检索TotalAmount元素?大@我被困在那里了。

下面是返回完整响应的代码的尾部:

$response = curl_exec($ch);
$xml_response = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children();
echo '<pre>';
print_r($xml_response);
echo '</pre>';

在试图得到TotalAmount价值

$postage = $xml_response->CalculatePostageRateResponse->PostageRateResponse->PostagePrice->Postage

这将检索我想要的数据部分,但我不知道如何访问[@attributes]阵列。

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [TotalAmount] => 8.15
        )

    [MailService] => Priority Mail
    [Zone] => 5
    [IntraBMC] => false
    [Pricing] => CommercialBase
)

这是整个$XML_答复:

SimpleXMLElement Object
(
    [CalculatePostageRateResponse] => SimpleXMLElement Object
        (
            [PostageRateResponse] => SimpleXMLElement Object
                (
                    [Status] => 0
                    [Zone] => 5
                    [PostagePrice] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [TotalAmount] => 8.15
                                )

                            [MailClass] => Priority
                            [Postage] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [TotalAmount] => 8.15
                                        )

                                    [MailService] => Priority Mail
                                    [Zone] => 5
                                    [IntraBMC] => false
                                    [Pricing] => CommercialBase
                                )

                            [Fees] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [TotalAmount] => 0
                                        )

                                    [CertificateOfMailing] => 0
                                    [CertifiedMail] => 0
                                    [CollectOnDelivery] => 0
                                    [DeliveryConfirmation] => 0
                                    [ElectronicReturnReceipt] => 0
                                    [InsuredMail] => 0
                                    [RegisteredMail] => 0
                                    [RestrictedDelivery] => 0
                                    [ReturnReceipt] => 0
                                    [ReturnReceiptForMerchandise] => 0
                                    [SignatureConfirmation] => 0
                                    [SpecialHandling] => 0
                                    [MerchandiseReturn] => 0
                                    [OpenAndDistribute] => 0
                                    [AdultSignature] => 0
                                    [AdultSignatureRestrictedDelivery] => 0
                                    [LiveAnimalSurcharge] => 0
                                    [FragileHandling] => 0
                                    [AMDelivery] => 0
                                    [GroupedExtraServices] => SimpleXMLElement Object
                                        (
                                            [@attributes] => Array
                                                (
                                                    [Services] => 
                                                )

                                            [FeeAmount] => 0
                                        )

                                )

                            [DeliveryTimeDays] => 2
                            [EstimatedDeliveryDate] => 6/1/2018
                            [DdpRate] => 0
                        )

                )

        )

)
提问于
用户回答回答于
$postage = $xml_response->CalculatePostageRateResponse->PostageRateResponse->PostagePrice->Postage[0]['TotalAmount'];

扫码关注云+社区

领取腾讯云代金券