首页 > 解决方案 > 如何解析客户端-> __getLastResponse?

问题描述

try {
    $resp = $client->__soapCall("shipmentTracking", array('shipmentTracking'=>array("search" => $request)));
    return $resp;
} catch (SoapFault $ex) {
    $responseXML = ($client->__getLastResponse());
    return $responseXML;
}

当我打印这个 responseXML 时,我在页面上看到了一个字符串结果,但是查看源代码向我显示了 XML。但是,当我对其执行 gettype() 时,它会将类型显示为“字符串”。我想解析响应并从中提取数据。如果我没有 XML 格式,我不明白该怎么做?

XML 响应 -

 "<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><ser-root:shipmentTrackingResponse xmlns:ser-root="https://api.estes-express.com/ws/tools/shipment/tracking/v1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><trackingInfo><requestID>1</requestID><shipments><shipments><pro>1760369125</pro><bol>502260371</bol><po>00847007086241</po><pickupDate>2020-10-23</pickupDate><status>Delivered</status><eventTimeStamp>2020-10-27T17:05:00-0400    </eventTimeStamp><movementHistory> </movementHistory><estimatedDeliveryDate>2020-10-27</estimatedDeliveryDate><estimatedDeliveryTime>12:05:00</estimatedDeliveryTime><receivedBy>JAMISON</receivedBy><appointment><apptDate></apptDate><apptTime></apptTime><status>                                                  </status></appointment><pieces>1</pieces><dimensionalWeight></dimensionalWeight><weight>707</weight><shipper><referenceNumber></referenceNumber><name>DAE LEE</name><address><line1>33 JULIA LANE</line1><line2></line2><city>EAST NORTHPORT</city><stateProvince>NY</stateProvince><postalCode>11731</postalCode><countryCode></countryCode></address></shipper><consignee><referenceNumber></referenceNumber><name>YARDISTRY - SONWIL</name><address><line1>1289 WALDEN AVE</line1><line2></line2><city>BUFFALO</city><stateProvince>NY</stateProvince><postalCode>14211</postalCode><countryCode></countryCode></address></consignee><thirdParty><referenceNumber></referenceNumber><name></name><address><line1></line1><line2></line2><city></city><stateProvince></stateProvince><postalCode></postalCode><countryCode>  </countryCode></address></thirdParty><destinationTerminal><number>083</number><name>BNY </name><address><line1>3680 Jeffrey Boulevard</line1><line2> </line2><city>Blasdell</city><stateProvince>NY</stateProvince><postalCode>14219</postalCode><countryCode> </countryCode></address><phone><country></country><areaCode>716</areaCode><subscriber>8260963</subscriber><extenstion> </extenstion></phone><fax><areaCode>716</areaCode><subscriber>8241409</subscriber></fax></destinationTerminal><interlineInfo><scac></scac><name></name><type></type></interlineInfo><freightCharges>319.08</freightCharges><messages><message>Freight Charges are shown on shipments for which the logged in user is the payor of the freight charges.</message><message>Freight Charges are subject to change upon audit.</message><message>Reported delivery time is subject to change based upon verification.</message><message></message></messages></shipments></shipments></trackingInfo></ser-root:shipmentTrackingResponse></soapenv:Body></soapenv:Envelope>"

标签: php

解决方案


如果要返回数组,可以执行以下操作:

try {
    return $client->__soapCall("shipmentTracking", array('shipmentTracking'=>array("search" => $request)));
} catch (SoapFault $ex) {
    return json_decode(json_encode((array)simplexml_load_string($client->__getLastResponse())),true)
}

您可以跳过该json_decode(json_encode())部分并仅转换为数组,但您可能会冒嵌套对象未正确转换的风险。

有关更多信息,请参阅https://stackoverflow.com/a/20524606/8108407上的评论。


推荐阅读