dpicx06888 2014-10-22 18:05
浏览 68
已采纳

如何阅读UPS网络服务的响应?

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 "
".__FILE__.' '.__LINE__."   xmlarray   
";  
\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;
  • 写回答

1条回答 默认 最新

  • doujuan2688 2014-10-22 20:23
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算