FedEx API:仅使用邮政编码获取运输时间

FexEx API: Getting Transit Times with only ZIP codes

我一直在网上搜索并通读 FedEx Web 服务开发人员指南,看看是否可以使用他们的 API 提交几个邮政编码和 return 估计的运输时间对于地面包。类似于 FedEx 的 "Get Rates & Transit Times" 页面所做的。有人做过这样的事吗?

为此,您需要使用 FedEx 的 "Validation Availability and Commitment Service"。这是他们使用 PDF 的 link:https://www.fedex.com/templates/components/apps/wpor/secure/downloads/pdf/201607/FedEx_WebServices_ValidationAvailabilityAndCommitmentServices_WSDLGuide_v2016.pdf

我也一直在研究它,并整理了一小段 PHP 有效的代码。

<?php

  // This is the local path to the FedEx Validation Availability and Commitment Service WSDL
  $_wsdlPath = '/xxxxxx/xxxxx/xxxxx/ValidationAvailabilityAndCommitmentService_v6.wsdl'; 

  $ship_from_zip = 10001;
  $ship_to_zip = 90210;

  // Create the SOAP client
  try {
    $client = new SoapClient($_wsdlPath, array('trace' => 1));
  }
  catch(SoapFault $soapFault) {
    print($soapFault);
    return;
  }

  // Create our request that we will send to FedEx
  $transit_time_request = createTransitTimeRequest($ship_from_zip, $ship_to_zip);

  // Send our request to FedEx
  $transit_time_response = callTransitTimeRequest($client, $transit_time_request);

  print_r($transit_time_response);

  function callTransitTimeRequest($client, $transit_time_request) {
    try {
      // Call FedEx's serviceAvailability operation
      $response = $client->serviceAvailability($transit_time_request);
      return $response;
    }
    catch(SoapFault $soapFault) {
      print($soapFault);
      return null;
    }
  }

  function createTransitTimeRequest($ship_from_zip, $ship_to_zip) {
    $_meterNumber = 'xxxxxx'; // Your accounts ClientDetail MeterNumber
    $_accountNumber = 'xxxxx'; // You accounts ClientDetail AccountNumber
    $_password = 'xxxxxx'; // Your accounts UserCredential Password
    $_key = 'xxxxx'; // Your accounts UserCredential Key
    $_majorVersion = X; // The WSDL version

    $request['WebAuthenticationDetail'] = array();
    $request['WebAuthenticationDetail']['UserCredential'] = array();
    $request['WebAuthenticationDetail']['UserCredential']['Key'] = $_key;
    $request['WebAuthenticationDetail']['UserCredential']['Password'] = $_password;
    $request['ClientDetail'] = array();
    $request['ClientDetail']['AccountNumber'] = $_accountNumber;
    $request['ClientDetail']['MeterNumber'] = $_meterNumber;
    $request['TransactionDetail'] = array();
    $request['TransactionDetail']['CustomerTransactionId'] = 'ServiceAvailabilityRequest';
    $request['Version'] = array();
    $request['Version']['ServiceId'] = 'vacs';
    $request['Version']['Major'] = $_majorVersion;
    $request['Version']['Intermediate'] = '0';
    $request['Version']['Minor'] = '0';
    $request['Origin'] = array();
    $request['Origin']['PostalCode'] = $ship_from_zip;
    $request['Origin']['CountryCode'] = 'US';
    $request['Destination'] = array();
    $request['Destination']['PostalCode'] = $ship_to_zip;
    $request['Destination']['CountryCode'] = 'US';
    $request['ShipDate'] = date("Y-m-d");
    $request['CarrierCode'] = "FDXG"; // FDXG = Ground, FDXE = Express

    return $request;
  }

?>

此外,这就是上面的代码 returns 以及您可以期待的内容:

stdClass Object
(
    [HighestSeverity] => NOTE
    [Notifications] => stdClass Object
        (
            [Severity] => NOTE
            [Source] => vacs
            [Code] => 2002
            [Message] => Your Packaging was assumed.
            [LocalizedMessage] => Your Packaging was assumed.
        )

    [TransactionDetail] => stdClass Object
        (
            [CustomerTransactionId] => ServiceAvailabilityRequest
        )

    [Version] => stdClass Object
        (
            [ServiceId] => vacs
            [Major] => 6
            [Intermediate] => 0
            [Minor] => 0
        )

    [Options] => Array
        (
            [0] => stdClass Object
                (
                    [Service] => FEDEX_GROUND
                    [TransitTime] => FOUR_DAYS
                )

            [1] => stdClass Object
                (
                    [Service] => GROUND_HOME_DELIVERY
                    [TransitTime] => FOUR_DAYS
                )

        )

)