php - 如何解析客户端-> __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>"
解决方案
如果要返回数组,可以执行以下操作:
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上的评论。
推荐阅读
- string - 来自字符串的 Delphi Hash - 小菜一碟?
- pine-script - 一开始就声明变量
- javascript - 通过 React 功能组件将功能从父级传递给子级
- python - 如何将多个 .npy 文件加载到单个 numpy 数组中
- django - ModelForm 没有将任何数据保存到数据库 - Django
- zsh - zsh 参数扩展,过滤掉与模式匹配的数组元素
- android - Flutter 轻松加载,如何编辑它的文字样式(下划线、字体系列、大小)?
- git - 将功能分支与开发分支同步
- swift - 尝试拨打号码时Swift NSOSStatusErrorDomain
- c++ - 不能为类成员调用函数