https://annasheachocolates.com/ratecheck.php
请检查上面的链接。它包含获取xml文件的详细信息。这是我目前正在使用的文件
我还附上了以下php文件和输出xml文件,供您参考。我要获取的是UPS地面/ UPS第二天航空/ UPS隔夜的三个货运费率值,但我得到的值都是相同的值。请确认我是否缺少什么。
<?php
try {
// configuration
$access = "XXXX";
$userid = "cxccxcxcx";
$passwd = "vvxcxcxcx";
/*$accessLicenseNumber = "XXXXXX";
$userId = "xxxx";
$password = "xxxx";
*/
$endpointurl = "https://onlinetools.ups.com/ups.app/xml/Rate";
$outputFileName = "XOLTResult.xml";
// create a simple xml object for AccessRequest & RateRequest
$accessRequesttXML = new SimpleXMLElement ( "< AccessRequest> < /AccessRequest> " );
$rateRequestXML = new SimpleXMLElement ( "< RatingServiceSelectionRequest> < /RatingServiceSelectionRequest> " );
// create AccessRequest XML
$accessRequesttXML-> addChild ( "AccessLicenseNumber", $access );
$accessRequesttXML-> addChild ( "UserId", $userid );
$accessRequesttXML-> addChild ( "Password", $passwd );
// create RateRequest XML
$request = $rateRequestXML-> addChild ( 'Request' );
$request-> addChild ( "RequestAction", "Rate" );
$request-> addChild ( "RequestOption", "Rate" );
$shipment = $rateRequestXML-> addChild ( 'Shipment' );
$shipper = $shipment-> addChild ( 'Shipper' );
$shipper-> addChild ( "Name", "Name" );
$shipper-> addChild ( "ShipperNumber", "" );
$shipperddress = $shipper-> addChild ( 'Address' );
$shipperddress-> addChild ( "AddressLine1", "Address Line" );
$shipperddress-> addChild ( "City", "Corado" );
$shipperddress-> addChild ( "PostalCode", "00646" );
$shipperddress-> addChild ( "CountryCode", "PR" );
$shipTo = $shipment-> addChild ( 'ShipTo' );
$shipTo-> addChild ( "CompanyName", "Company Name" );
$shipToAddress = $shipTo-> addChild ( 'Address' );
$shipToAddress-> addChild ( "AddressLine1", "Address Line" );
$shipToAddress-> addChild ( "City", "Corado" );
$shipToAddress-> addChild ( "PostalCode", "00646" );
$shipToAddress-> addChild ( "CountryCode", "PR" );
$shipFrom = $shipment-> addChild ( 'ShipFrom' );
$shipFrom-> addChild ( "CompanyName", "Company Name" );
$shipFromAddress = $shipFrom-> addChild ( 'Address' );
$shipFromAddress-> addChild ( "AddressLine1", "Address Line" );
$shipFromAddress-> addChild ( "City", "Boca Raton" );
$shipFromAddress-> addChild ( "StateProvinceCode", "FL" );
$shipFromAddress-> addChild ( "PostalCode", "33434" );
$shipFromAddress-> addChild ( "CountryCode", "US" );
$service = $shipment-> addChild ( 'Service' );
$service-> addChild ( "Code", "02" );
$service-> addChild ( "Description", "2nd Day Air" );
$package = $shipment-> addChild ( 'Package' );
$packageType = $package-> addChild ( 'PackagingType' );
$packageType-> addChild ( "Code", "02" );
$packageType-> addChild ( "Description", "UPS Package" );
$packageWeight = $package-> addChild ( 'PackageWeight' );
$unitOfMeasurement = $packageWeight-> addChild ( 'UnitOfMeasurement' );
$unitOfMeasurement-> addChild ( "Code", "LBS" );
$packageWeight-> addChild ( "Weight", "15.2" );
$requestXML = $accessRequesttXML-> asXML () . $rateRequestXML-> asXML ();
echo($requestXML);
// create Post request
$form = array (
'http' => array (
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => "$requestXML"
)
);
$request = stream_context_create ( $form );
$browser = fopen ( $endpointurl, 'rb', false, $request );
if (! $browser) {
throw new Exception ( "Connection failed." );
}
// get response
$response = stream_get_contents ( $browser );
fclose ( $browser );
if ($response == false) {
throw new Exception ( "Bad data." );
} else {
// save request and response to file
$fw = fopen ( $outputFileName, 'w' );
fwrite ( $fw, "Request: \n" . $requestXML . "\n" );
fwrite ( $fw, "Response: \n" . $response . "\n" );
fclose ( $fw );
// get response status
$resp = new SimpleXMLElement ( $response );
echo("das".$response);
//echo $resp-> Response-> ResponseStatusDescription . "\n";
}
} catch ( Exception $ex ) {
echo $ex;
}
?>
*** XML代码****
<?xml version="1.0"?>
<RatingServiceSelectionResponse>
<Response>
<ResponseStatusCode>1</ResponseStatusCode>
<ResponseStatusDescription>Success</ResponseStatusDescription>
</Response>
<RatedShipment>
<Service>
<Code>02</Code>
</Service>
<RatedShipmentWarning>Your
invoice may vary from the displayed reference
rates</RatedShipmentWarning>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>16.0</Weight>
</BillingWeight>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>109.97</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>109.97</MonetaryValue>
</TotalCharges>
<GuaranteedDaysToDelivery>2</GuaranteedDaysToDelivery>
<ScheduledDeliveryTime/>
<RatedPackage>
<TransportationCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>109.97</MonetaryValue>
</TransportationCharges>
<ServiceOptionsCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>0.00</MonetaryValue>
</ServiceOptionsCharges>
<TotalCharges>
<CurrencyCode>USD</CurrencyCode>
<MonetaryValue>109.97</MonetaryValue>
</TotalCharges>
<Weight>15.2</Weight>
<BillingWeight>
<UnitOfMeasurement>
<Code>LBS</Code>
</UnitOfMeasurement>
<Weight>16.0</Weight>
</BillingWeight>
</RatedPackage>
</RatedShipment>
</RatingServiceSelectionResponse>
结果 地址行Corado00646PR 公司名 地址行Corado00646PR 公司名 地址行博卡拉顿FL33434US 022nd Day Air02UPS包裹LBS15.2 das 1成功02您的发票可能会与显示的参考汇率有所不同LBS16.0USD109.97USD0.00USD109.972USD109.97USD0.00USD109.9715.2LBS16.0