【问题标题】:Getting the rate of a specific service - EasyPost获取特定服务的费率 - EasyPost
【发布时间】:2017-04-03 17:04:14
【问题描述】:

我目前有 EasyPost 创建货件。当我创建货件时,它会为我提供所有可用的服务和费率。

我想动态选择服务并只返回该费率。

我最初会使用索引号读取数组(我猜应该如何描述它)。

问题是每次我创建一个新货件时,rates 数组的顺序都会发生变化,因此 $shipment->rates[0]['rate'] 将是快递,然后是下一次头等舱.

我想使用“快递”创建一个货件,并且只退回该费率。

任何建议将不胜感激。

这是我的代码:

$name = $this->thiscustomer->cust_first . ' ' . $this->thiscustomer->cust_first;
$street_1 = $this->thiscustomer->street_1;
$street_2 = $this->thiscustomer->street_2;
$city = $this->thiscustomer->city;
$state = $this->thiscustomer->state;
$zip = $this->thiscustomer->zip;


$weight = $this->weight;
    $packaging = $this->packaging;
    $service = $this->service;
    $caddress = $this->consultant->address;
    $cstreet_1 = $this->consultant->street_1;
    $cstreet_2 = $this->consultant->street_2;
    $ccity = $this->consultant->city;
    $cstate = $this->consultant->state;
    $czip = $this->consultant->zip;
    $cname = $this->consultant->first_name . ' ' . $this->consultant->last_name;
    $cuser_id = $this->consultant->user_id;




require_once(dirname(__FILE__) . '/lib/easypost.php');
\EasyPost\EasyPost::setApiKey('KUk4fZUI6YaYc1h0FiIXFw');


$shipment = \EasyPost\Shipment::create(array(
 'to_address' => array(
"name"    => $name,
"street1" => $street_1,
"street2" => $street_2,
"city"    => $city,
"state"   => $state,
"zip"     => $zip
 ),
 'from_address' => array(
"company" => $cname,
"street1" => $cstreet_1,
"street2" => $cstreet_2,
"city"    => $ccity,
"state"   => $cstate,
"zip"     => $czip
 ),
 'parcel' => array(
'weight' => $weight,
'predefined_package'=> $packaging
),
'rates' => array(
  'service' => $service
)

));

echo $shipment->rates[0]['rate']. '<br>';

【问题讨论】:

    标签: php easypost


    【解决方案1】:

    在 EasyPost 的Getting Started Guide 中有一个购买特定运营商+费率的示例:

    $shipment->buy($shipment->lowest_rate(array('USPS'), array('Express')));
    

    【讨论】:

    • 在我知道价格是多少之前我不想买它。
    • 在这种情况下,您只需要进行一些数组过滤。除了选择承运人(您可以使用 carrier_accounts 字段)之外,这些详细信息是 EasyPost 有意避开其便利方法的业务逻辑,以便您准确确定您想要什么。见php.net/manual/en/function.array-filter.php
    【解决方案2】:

    您可以通过以下代码行获得不同的费率

    if(!empty($shipment->rates))
    {
    $count = 0;
    foreach($shipment->rates as $rate)
    { 
    $serviceRates[$count] = [
    'service' => $rate->service,
    'carrier' => $rate->carrier,
    'rate' => $rate->rate,
    'currency' => $rate->currency,
    'delivery_days' => $rate->delivery_days
    ];
    $count++;
    }
    }
    else
    {
    echo 'no result fond for given address';
    }
          print_r($serviceRates);
    

    【讨论】:

      猜你喜欢
      • 2014-02-17
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多