首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取特定服务的速率- EasyPost

获取特定服务的速率- EasyPost
EN

Stack Overflow用户
提问于 2017-04-04 01:04:15
回答 2查看 533关注 0票数 0

我目前已经让EasyPost创建了一个货件。当我创建货件时,它为我提供了所有可用的服务和费率。

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

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

这样做的问题是,每次我创建新的发货时,rates数组的顺序都会发生变化,因此$ shipment ->rates应该是express的,然后下一次是first class。

我想用“快递”创建一个货件,并且只退货。

任何建议都将不胜感激。

下面是我的代码:

代码语言:javascript
复制
$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>';
EN

回答 2

Stack Overflow用户

发布于 2017-04-04 03:49:37

在EasyPost的Getting Started Guide中,有一个购买特定carrier+rate的示例:

代码语言:javascript
复制
$shipment->buy($shipment->lowest_rate(array('USPS'), array('Express')));
票数 2
EN

Stack Overflow用户

发布于 2021-02-03 18:09:05

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43190393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档