我目前有EasyPost创建一个货件。当我创建货件时,它为我提供所有可用的服务和费率。
我希望动态选择服务并仅返回费率。
我最初会使用索引号通读数组(我猜这是如何描述它的)。
这样做的问题是,每次我创建一个新的装运,rates数组的顺序都会改变,所以本来应该是$shipment-〉rates[0]['rate']的内容将是express,然后下一次是first class。
我想创建一个货件“快速”,并只返回该费率。
如有任何建议,我们将不胜感激。
下面是我的代码:
$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>';
3条答案
按热度按时间t40tm48m1#
在EasyPost的《入门指南》中,有一个购买特定运营商+费率的示例:
7kqas0il2#
您可以通过以下代码行获得不同的费率
lc8prwob3#
@KFunk说得对,EasyPost没有提供一种本地方式来完成你所要求的任务,因为这个用例对你来说是独一无二的。EasyPost客户端库提供了与API交互所需的所有函数;但是,所有的业务逻辑都留给用户。
如果您要创建一个装运,但只向用户显示一个费率,则需要在您自己的应用程序中筛选出其他费率。为此,您可以按照下面的简单示例操作:
依赖于数组排序是一种应该避免的反模式。数组索引永远不能保证保持相同的顺序,并且与所涉及的数据没有很强的联系。
如需EasyPost API的进一步帮助,请通过support@easypost.com联系我们的支持团队。祝您好运!