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

When I print this responseXML, I see a string result on the page, but view source shows me the XML. However, when I did gettype() on it, it shows type as "string". I want to parse the response and extract data out of it. I dont understand how can I do that if I dont have an XML format?

XML Response -

 "<?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>"
Mihir
  • 43
  • 6
  • Well, XML is actually text so it is a string. Or do you want a `SimpleXML` object? – jrswgtr Nov 16 '20 at 16:58
  • Yeah, I want to have a XML Object or even a JSON is fine, just so as I could get data using some Key. I will add the XML Response as well – Mihir Nov 16 '20 at 16:59
  • I think this post may help you https://stackoverflow.com/questions/6578832/how-to-convert-xml-into-array-in-php – jrswgtr Nov 16 '20 at 17:00
  • I tried this as well - $responseXML = new SimpleXMLElement($client->__getLastResponse()); return json_decode(json_encode($responseXML)); All combinations of it, then when I try to print I still dont see an array – Mihir Nov 16 '20 at 17:02
  • `$array = json_decode(json_encode((array)simplexml_load_string($client->__getLastResponse())),true);` as in https://stackoverflow.com/a/20524606/8108407 should do the trick for you – jrswgtr Nov 16 '20 at 17:03
  • It didnt, it created an empty array, just like SimpleXML was creating an empty XML Object – Mihir Nov 16 '20 at 17:07

1 Answers1

0

If you want to return an array you can do as follows:

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)
}

You can skip the json_decode(json_encode()) part and only cast to array but then you risk that nested objects are not converted properly.

See the comments at https://stackoverflow.com/a/20524606/8108407 for more information.

jrswgtr
  • 2,287
  • 8
  • 23
  • 49