Reputation: 483
I am trying to parse the UPS sample code for the Rating service and while I get the response from UPS, I cannot seem to do anything with it. How do I read through and find the data I want? eg
<rate:RatedShipment><rate:Service><rate:Code>03</rate:Code>
and
<rate:TotalCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>126.72</rate:MonetaryValue>
</rate:TotalCharges>
I need that code and the total charges. My code so far looks like this:
$result = $client->__soapCall($operation ,array($this->processRate()));
$resp = $client->__getLastResponse() ;
echo $resp ;
output:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header />
<soapenv:Body>
<rate:RateResponse xmlns:rate="http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1">
<common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
<common:Alert>
<common:Code>110971</common:Code>
<common:Description>Your invoice may vary from the displayed reference rates</common:Description>
</common:Alert>
<common:Alert>
<common:Code>110920</common:Code>
<common:Description>Ship To Address Classification is changed from Residential to Commercial</common:Description>
</common:Alert>
<common:TransactionReference />
</common:Response>
<rate:RatedShipment>
<rate:Service>
<rate:Code>03</rate:Code>
<rate:Description />
</rate:Service>
<rate:RatedShipmentAlert>
<rate:Code>110971</rate:Code>
<rate:Description>Your invoice may vary from the displayed reference rates</rate:Description>
</rate:RatedShipmentAlert>
<rate:RatedShipmentAlert>
<rate:Code>110920</rate:Code>
<rate:Description>Ship To Address Classification is changed from Residential to Commercial</rate:Description>
</rate:RatedShipmentAlert>
<rate:BillingWeight>
<rate:UnitOfMeasurement>
<rate:Code>LBS</rate:Code>
<rate:Description>Pounds</rate:Description>
</rate:UnitOfMeasurement>
<rate:Weight>3.0</rate:Weight>
</rate:BillingWeight>
<rate:TransportationCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>17.76</rate:MonetaryValue>
</rate:TransportationCharges>
<rate:ServiceOptionsCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>0.00</rate:MonetaryValue>
</rate:ServiceOptionsCharges>
<rate:TotalCharges>
<rate:CurrencyCode>USD</rate:CurrencyCode>
<rate:MonetaryValue>17.76</rate:MonetaryValue>
</rate:TotalCharges>
</rate:RatedShipment>
......
</rate:RateResponse>
</soapenv:Body>
</soapenv:Envelope>
I then run this code on it:
$xml = simplexml_load_string($resp);
\TYPO3\Flow\var_dump($xml );
$json = json_encode($xml);
$xmlarray = json_decode($json,TRUE);
echo "\n".__FILE__.' '.__LINE__." xmlarray \n";
\TYPO3\Flow\var_dump($xmlarray);
die;
This outputs
<div class="Flow-Error-Debugger-VarDump Flow-Error-Debugger-VarDump-Floating">
<div class="Flow-Error-Debugger-VarDump-Top">
Flow Variable Dump
</div>
<div class="Flow-Error-Debugger-VarDump-Center">
<pre dir="ltr"><span class="debug-object debug-unregistered" title="00000000366333b30000000074e4ee7f">SimpleXMLElement</span><span class="debug-scope">prototype<a id="o00000000366333b30000000074e4ee7f"></a></span><span class="debug-ptype" title="unknown">object</span></pre>
</div>
</div>
/home/me/domains/shop.me.com/public_html/releases/20131219160416/Data/Temporary/Development/Cache/Code/Flow_Object_Classes/Shop_Shipping_UPSShippingHandler.php 336 xmlarray
<div class="Flow-Error-Debugger-VarDump Flow-Error-Debugger-VarDump-Floating">
<div class="Flow-Error-Debugger-VarDump-Top">
Flow Variable Dump
</div>
<div class="Flow-Error-Debugger-VarDump-Center">
<pre dir="ltr">array(empty)</pre>
</div>
</div>
Seems my simplexml_load_string($resp)
call is not working.
UPDATE: Thanks alien. This is what my code looks like now - it is not perfect yet but you get the idea - :
$result = $client->__soapCall($operation ,array($this->processRate()));
$resp = $client->__getLastResponse() ;
$sxe = simplexml_load_string($resp);
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
$mv = $sxe->xpath('//r:TotalCharges/r:MonetaryValue');
$indx=0;
$estimates = array();
foreach ($codes as $c) {
$temporaryEstimate = array();
$temporaryEstimate['estimateId'] = $this->getServiceTitleByCode($c->__toString());
$temporaryEstimate['estimatePrice'] = '$' .$mv[$indx];
$estimates[] = $temporaryEstimate;
$indx++;
}
return $estimates;
Upvotes: 0
Views: 961
Reputation: 9520
There is no real need to do the conversion to JSON and back again (unless you like making your computer work!); I loaded the XML you posted directly into a SimpleXMLElement and then queried it using XPath:
$sxe = new SimpleXMLElement($resp);
The elements that you are interested in are all in the rate
namespace, so you need to register the namespace to be able to query it:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
We can now refer to all the rate:
nodes in the XML using the shorthand r:
.
Query for all the Code
elements under RatedShipment/Service
in the r
namespace:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
Output:
code: 03
Find the total cost of the shipment, which is under r:TotalCharges
as r:MonetaryValue
and r:CurrencyCode
:
# xpath returns an array; we want the first element of that array for both these
$curr = $sxe->xpath('//r:TotalCharges/r:CurrencyCode')[0];
$mv = $sxe->xpath('//r:TotalCharges/r:MonetaryValue')[0];
echo "total charges: $mv $curr ". PHP_EOL;
Output:
total charges: 17.76 USD
If you aren't familiar with XPath, see the PHP documentation for more information on SimpleXML's XPath implementation.
Upvotes: 4