微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Fedex Rate API请求

我需要创建一个简单的网页,从FedEx获取报价.唯一的问题是,我以前从未搞过API.

XML很简单,但如何将该XML发送到FedEx并查看响应? API请求…是的我知道,但是对FedEx的API请求的代码是什么?我只需要一些指导.我在某种程度上了解PHP – 但是,我不是专家.

我知道我需要发送一个API请求,但我需要一个使用PHP的简单工作示例.我希望能够输入我的帐户信息,然后有一个简单的工作率报价.

我不在乎它是否只返回最简单的数据.我只需要有一些东西让我开始.

看起来好像FedEx在提供用PHP这样做的信息方面走得那么远.

解决方法:

fedex在www.fedex.com/us/developer/中提供加速包,您可以找到有关不同类型的Web服务调用的信息.例如,如果您想从fedex请求费率,您需要执行以下操作:

<?PHP

require_once('../../library/fedex-common.PHP5');

$newline = "<br />";
//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.
$path_to_wsdl = "../../wsdl/RateService_v13.wsdl";

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.PHP.net/manual/en/ref.soap.PHP for more information

$request['WebAuthenticationDetail'] = array(
    'UserCredential' =>array(
        'Key' => getProperty('key'), 
        'Password' => getProperty('password')
    )
); 
$request['ClientDetail'] = array(
    'AccountNumber' => getProperty('shipaccount'), 
    'MeterNumber' => getProperty('meter')
);
$request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Request v13 using PHP ***');
$request['Version'] = array(
    'ServiceId' => 'crs', 
    'Major' => '13', 
    'Intermediate' => '0', 
    'Minor' => '0'
);
$request['ReturnTransitAndCommit'] = true;
$request['RequestedShipment']['DropoffType'] = 'REGULAR_PICKUP'; // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
$request['RequestedShipment']['ShipTimestamp'] = date('c');
$request['RequestedShipment']['ServiceType'] = 'INTERNATIONAL_PRIORITY'; // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
$request['RequestedShipment']['PackagingType'] = 'YOUR_PACKAGING'; // valid values FEDEX_Box, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
$request['RequestedShipment']['TotalInsuredValue']=array('Ammount'=>100,'Currency'=>'USD');
$request['RequestedShipment']['Shipper'] = addShipper();
$request['RequestedShipment']['Recipient'] = addRecipient();
$request['RequestedShipment']['ShippingChargesPayment'] = addShippingChargesPayment();
$request['RequestedShipment']['RateRequestTypes'] = 'ACCOUNT'; 
$request['RequestedShipment']['RateRequestTypes'] = 'LIST'; 
$request['RequestedShipment']['PackageCount'] = '1';
$request['RequestedShipment']['RequestedPackageLineItems'] = addPackageLineItem1();
try 
{
    if(setEndpoint('changeEndpoint'))
    {
        $newLocation = $client->__setLocation(setEndpoint('endpoint'));
    }

    $response = $client ->getRates($request);

    if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR')
    {   
        $rateReply = $response -> RateReplyDetails;
        echo '<table border="1">';
        echo '<tr><td>Service Type</td><td>Amount</td><td>Delivery Date</td></tr><tr>';
        $serviceType = '<td>'.$rateReply -> ServiceType . '</td>';
        $amount = '<td>$' . number_format($rateReply->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount,2,".",",") . '</td>';
        if(array_key_exists('DeliveryTimestamp',$rateReply)){
            $deliveryDate= '<td>' . $rateReply->DeliveryTimestamp . '</td>';
        }else if(array_key_exists('TransitTime',$rateReply)){
            $deliveryDate= '<td>' . $rateReply->TransitTime . '</td>';
        }else {
            $deliveryDate='<td>&nbsp;</td>';
        }
        echo $serviceType . $amount. $deliveryDate;
        echo '</tr>';
        echo '</table>';

        printSuccess($client, $response);
    }
    else
    {
        printError($client, $response);
    } 

    writetoLog($client);    // Write to log file   

} catch (SoapFault $exception) {
   printFault($exception, $client);        
}

function addShipper(){
    $shipper = array(
        'Contact' => array(
            'PersonName' => 'Sender Name',
            'CompanyName' => 'Sender Company Name',
            'PhoneNumber' => '9012638716'),
        'Address' => array(
            'StreetLines' => array('Address Line 1'),
            'City' => 'Collierville',
            'StateOrProvinceCode' => 'TN',
            'PostalCode' => '38017',
            'CountryCode' => 'US')
    );
    return $shipper;
}
function addRecipient(){
    $recipient = array(
        'Contact' => array(
            'PersonName' => 'Recipient Name',
            'CompanyName' => 'Company Name',
            'PhoneNumber' => '9012637906'
        ),
        'Address' => array(
            'StreetLines' => array('Address Line 1'),
            'City' => 'Richmond',
            'StateOrProvinceCode' => 'BC',
            'PostalCode' => 'V7C4V4',
            'CountryCode' => 'CA',
            'Residential' => false)
    );
    return $recipient;                                      
}
function addShippingChargesPayment(){
    $shippingChargesPayment = array(
        'PaymentType' => 'SENDER', // valid values RECIPIENT, SENDER and THIRD_PARTY
        'Payor' => array(
            'ResponsibleParty' => array(
            'AccountNumber' => getProperty('billaccount'),
            'CountryCode' => 'US')
        )
    );
    return $shippingChargesPayment;
}
function addLabelSpecification(){
    $labelSpecification = array(
        'LabelFormatType' => 'COMMON2D', // valid values COMMON2D, LABEL_DATA_ONLY
        'ImageType' => 'PDF',  // valid values dpl, EPL2, PDF, ZPLII and PNG
        'LabelStockType' => 'PAPER_7X4.75');
    return $labelSpecification;
}
function addSpecialServices(){
    $specialServices = array(
        'SpecialServiceTypes' => array('COD'),
        'CodDetail' => array(
            'CodCollectionAmount' => array('Currency' => 'USD', 'Amount' => 150),
            'CollectionType' => 'ANY')// ANY, GUaraNTEED_FUNDS
    );
    return $specialServices; 
}
function addPackageLineItem1(){
    $packageLineItem = array(
        'SequenceNumber'=>1,
        'GroupPackageCount'=>1,
        'Weight' => array(
            'Value' => 50.0,
            'Units' => 'LB'
        ),
        'Dimensions' => array(
            'Length' => 108,
            'Width' => 5,
            'Height' => 5,
            'Units' => 'IN'
        )
    );
    return $packageLineItem;
}

?>

所以去fedex.com,用库等下载wsdl或xml.运行此代码,您将收到报价.重要的是说你需要一个帐户才能访问该区域,在那里你会收到一个测试计量帐户来尝试,然后转移到生产..希望它有所帮助.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐