XML API响应中的Access数组转换为PHP对象

时间:2018-05-30 22:16:08

标签: php xml soap

有谁知道如何访问SOAP API返回的SimpleXMLElement对象中的[@attributes]数组?

我未能检索TotalAmount数组的[@attributes]元素的值。 @是我被困的地方。

这里是返回完整响应的代码的尾端:

$response = curl_exec($ch);
$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOWARNING);
$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_response:

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
                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

<强>解决方案

$postage = $xml_response->CalculatePostageRateResponse->PostageRateResponse->PostagePrice->Postage[0]['TotalAmount'];