FedEx API 无法在 PHP 中正确计算费率

fedex API doesnot calculate rates correctly in PHP

本文关键字:计算 费率 PHP API FedEx      更新时间:2023-09-26

我想在PHP网站中应用FedEx API。我做到了,但我的客户说它给出了错误的费率。我的代码如下:

       // FedEx
$services['fedex']['PRIORITYOVERNIGHT'] = 'Priority Overnight';
$services['fedex']['STANDARDOVERNIGHT'] = 'Standard Overnight';
$services['fedex']['FIRSTOVERNIGHT'] = 'First Overnight';
$services['fedex']['FEDEX2DAY'] = '2 Day';
$services['fedex']['FEDEXEXPRESSSAVER'] = 'Express Saver';
$services['fedex']['FEDEXGROUND'] = 'Ground';
$services['fedex']['FEDEX1DAYFREIGHT'] = 'Overnight Day Freight';
$services['fedex']['FEDEX2DAYFREIGHT'] = '2 Day Freight';
$services['fedex']['FEDEX3DAYFREIGHT'] = '3 Day Freight';
$services['fedex']['GROUNDHOMEDELIVERY'] = 'Home Delivery';
$services['fedex']['INTERNATIONALECONOMY'] = 'International Economy';
$services['fedex']['INTERNATIONALFIRST'] = 'International First';
$services['fedex']['INTERNATIONALPRIORITY'] = 'International Priority';
// Config
$config = array(
    // Services
     'services' => $services,
    // Weight
    'weight' => 2, // Default = 1
    'weight_units' => 'lb', // lb (default), oz, gram, kg
    // Size
    'size_length' => 5, // Default = 8
    'size_width' => 6, // Default = 4
    'size_height' => 3, // Default = 2
    'size_units' => 'in', // in (default), feet, cm
    // From
    'from_zip' => 97210,
    'from_state' => "OR", // Only Required for FedEx
    'from_country' => "US",
    // To
    'to_zip' => 55455,
    'to_state' => "MN", // Only Required for FedEx
    'to_country' => "US",
    // Service Logins
    //'ups_access' => '0C2D05F55AF310D0', // UPS Access License Key
    //'ups_user' => 'dwstudios', // UPS Username 
    //'ups_pass' => 'dwstudios', // UPS Password 
    //'ups_account' => '81476R', // UPS Account Number
    //'fedex_account' =>'510087461', // FedEX Account Number
    //'fedex_meter' => '100173462' // FedEx Meter Number 
);

在我的运费计算器页面中:

// Calculate FedEX
    function calculate_fedex($code) {
    //print_R($this->weight);die;
       // $url = "https://gatewaybeta.fedex.com/GatewayDC";
        $url = "https://gateway.fedex.com/GatewayDC";
        $data = '<?xml version="1.0" encoding="UTF-8" ?>
<FDXRateRequest xmlns:api="http://www.fedex.com/fsmapi" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FDXRateRequest.xsd">
    <RequestHeader>
        <CustomerTransactionIdentifier>Express Rate</CustomerTransactionIdentifier>
        <AccountNumber>'.$this->fedex_account.'</AccountNumber>
        <MeterNumber>'.$this->fedex_meter.'</MeterNumber>
        <CarrierCode>'.(in_array($code,array('FEDEXGROUND','GROUNDHOMEDELIVERY')) ? 'FDXG' : 'FDXE').'</CarrierCode>
    </RequestHeader>
    <DropoffType>REGULARPICKUP</DropoffType>
    <Service>'.$code.'</Service>
    <Packaging>YOURPACKAGING</Packaging>
    <WeightUnits>LBS</WeightUnits>
    <Weight>'.number_format(($this->weight_unit != 'lb' ? convert_weight($this->weight,$this->weight_unit,'lb') : $this->weight), 1, '.', '').'</Weight>
    <OriginAddress>
        <StateOrProvinceCode>'.$this->from_state.'</StateOrProvinceCode>
        <PostalCode>'.$this->from_zip.'</PostalCode>
        <CountryCode>'.$this->from_country.'</CountryCode>
    </OriginAddress>
    <DestinationAddress>
        <StateOrProvinceCode>'.$this->to_state.'</StateOrProvinceCode>
        <PostalCode>'.$this->to_zip.'</PostalCode>
        <CountryCode>'.$this->to_country.'</CountryCode>
    </DestinationAddress>
    <Payment>
        <PayorType>SENDER</PayorType>
    </Payment>
    <PackageCount>1</PackageCount>
</FDXRateRequest>';
        // Curl
        $results = $this->curl($url,$data);
        // Debug
        if($this->debug == true) {
            print "<xmp>".$data."</xmp><br />";
            print "<xmp>".$results."</xmp><br />";
        }
        // Match Rate
        preg_match('/<NetCharge>(.*?)<'/NetCharge>/',$results,$rate);
        //print_R($rate[1]);die;
        return $rate[1];
    }

我的输出是:

Rates for sending a 2 lb, 5 x 6 x 3 in package from 97210 to 55455:
Array
(
    [fedex] => Array
        (
            [PRIORITYOVERNIGHT] => 35.46
            [STANDARDOVERNIGHT] => 32.93
            [FIRSTOVERNIGHT] => 96.47
            [FEDEX2DAY] => 16.87
            [FEDEXEXPRESSSAVER] => 11.69
            [FEDEXGROUND] => 6.71
            [FEDEX1DAYFREIGHT] => 
            [FEDEX2DAYFREIGHT] => 
            [FEDEX3DAYFREIGHT] => 
            [GROUNDHOMEDELIVERY] => 11.13
            [INTERNATIONALECONOMY] => 16.87
            [INTERNATIONALFIRST] => 96.47
            [INTERNATIONALPRIORITY] => 35.46
        )
)

smebody可以给我另一个FEDEX api的代码吗?提前致谢

开发人员帐户和生产帐户通常会给出不同的结果,因为客户的帐户可能会获得开发环境中不可用的折扣。 不久前我们遇到了这个问题,开发率远高于生产。