Php 如何从UPS web服务读取soap响应?
我正在尝试解析评级服务的UPS示例代码,虽然我得到了UPS的响应,但我似乎对此无能为力。如何通读并找到所需的数据?乙二醇Php 如何从UPS web服务读取soap响应?,php,xml,soap,ups,Php,Xml,Soap,Ups,我正在尝试解析评级服务的UPS示例代码,虽然我得到了UPS的响应,但我似乎对此无能为力。如何通读并找到所需的数据?乙二醇 <rate:RatedShipment><rate:Service><rate:Code>03</rate:Code> 输出: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmls
<rate:RatedShipment><rate:Service><rate:Code>03</rate:Code>
输出:
<?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>
这个输出
<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>
没有必要再转换成JSON(除非你喜欢让你的计算机工作!);我将您发布的XML直接加载到SimpleXMLElement中,然后使用XPath查询它:
$sxe = new SimpleXMLElement($resp);
您感兴趣的元素都在rate
名称空间中,因此您需要注册名称空间才能查询它:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
现在,我们可以使用速记r:
引用XML中的所有rate:
节点
查询r
命名空间中RatedShipment/Service
下的所有code
元素:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
输出:
code: 03
total charges: 17.76 USD
在r:TotalCharges
下查找装运的总成本,如r:MonetaryValue
和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;
输出:
code: 03
total charges: 17.76 USD
如果您不熟悉XPath,请参阅PHP文档以获取有关的详细信息。没有必要再转换为JSON(除非您喜欢让计算机工作!);我将您发布的XML直接加载到SimpleXMLElement中,然后使用XPath查询它:
$sxe = new SimpleXMLElement($resp);
您感兴趣的元素都在rate
名称空间中,因此您需要注册名称空间才能查询它:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
现在,我们可以使用速记r:
引用XML中的所有rate:
节点
查询r
命名空间中RatedShipment/Service
下的所有code
元素:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
输出:
code: 03
total charges: 17.76 USD
在r:TotalCharges
下查找装运的总成本,如r:MonetaryValue
和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;
输出:
code: 03
total charges: 17.76 USD
如果您不熟悉XPath,请参阅PHP文档以获取有关的详细信息。没有必要再转换为JSON(除非您喜欢让计算机工作!);我将您发布的XML直接加载到SimpleXMLElement中,然后使用XPath查询它:
$sxe = new SimpleXMLElement($resp);
您感兴趣的元素都在rate
名称空间中,因此您需要注册名称空间才能查询它:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
现在,我们可以使用速记r:
引用XML中的所有rate:
节点
查询r
命名空间中RatedShipment/Service
下的所有code
元素:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
输出:
code: 03
total charges: 17.76 USD
在r:TotalCharges
下查找装运的总成本,如r:MonetaryValue
和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;
输出:
code: 03
total charges: 17.76 USD
如果您不熟悉XPath,请参阅PHP文档以获取有关的详细信息。没有必要再转换为JSON(除非您喜欢让计算机工作!);我将您发布的XML直接加载到SimpleXMLElement中,然后使用XPath查询它:
$sxe = new SimpleXMLElement($resp);
您感兴趣的元素都在rate
名称空间中,因此您需要注册名称空间才能查询它:
$sxe->registerXPathNamespace('r', "http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1");
现在,我们可以使用速记r:
引用XML中的所有rate:
节点
查询r
命名空间中RatedShipment/Service
下的所有code
元素:
$codes = $sxe->xpath('//r:RatedShipment/r:Service/r:Code');
foreach ($codes as $c) {
echo "code: $c" . PHP_EOL;
}
输出:
code: 03
total charges: 17.76 USD
在r:TotalCharges
下查找装运的总成本,如r:MonetaryValue
和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;
输出:
code: 03
total charges: 17.76 USD
如果您不熟悉XPath,请参阅PHP文档以了解有关的详细信息。谢谢,我会这样做,我尝试使用Soap调用,但仍然不走运。谢谢,我会这样做,我尝试使用Soap调用,但仍然不走运。谢谢,我会这样做,我试着用Soap调用来实现它,但仍然不走运。谢谢,我会这样尝试,我试着用Soap调用来实现它,但仍然不走运。