php 获取特定服务的费率- EasyPost

ymdaylpp  于 2022-12-02  发布在  PHP
关注(0)|答案(3)|浏览(109)

我目前有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>';
t40tm48m

t40tm48m1#

在EasyPost的《入门指南》中,有一个购买特定运营商+费率的示例:

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

7kqas0il2#

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

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);
lc8prwob

lc8prwob3#

@KFunk说得对,EasyPost没有提供一种本地方式来完成你所要求的任务,因为这个用例对你来说是独一无二的。EasyPost客户端库提供了与API交互所需的所有函数;但是,所有的业务逻辑都留给用户。
如果您要创建一个装运,但只向用户显示一个费率,则需要在您自己的应用程序中筛选出其他费率。为此,您可以按照下面的简单示例操作:

<?php

\EasyPost\EasyPost::setApiKey($_ENV['EASYPOST_API_KEY']);

// Create your shipment, the response will contain all rates for all configured carriers
$shipment = \EasyPost\Shipment::create([
    'to_address' => [
        'name' => 'Dr. Steve Brule',
        'street1' => '179 N Harbor Dr',
        'city' => 'Redondo Beach',
        'state' => 'CA',
        'zip' => '90277',
        'country' => 'US',
        'phone' => '3331114444',
        'email' => 'dr_steve_brule@gmail.com'
    ],
    'from_address' => [
        'name' => 'EasyPost',
        'street1' => '417 Montgomery Street',
        'street2' => '5th Floor',
        'city' => 'San Francisco',
        'state' => 'CA',
        'zip' => '94104',
        'country' => 'US',
        'phone' => '3331114444',
        'email' => 'support@easypost.com'
    ],
    'parcel' => [
        'length' => 20.2,
        'width' => 10.9,
        'height' => 5,
        'weight' => 65.9
    ]
]);

$rates = $shipments->rates;

// Filter out all the rates that aren't the desired one, in this case - `Express`
$expressRate = null;
foreach ($rates as $rate) {
    if ($rate->service == "Express") {
        $expressRate = $rate;
        break;
    }
}

// Return $expressRate to your UI or do with it what you need
echo $expressRate;

依赖于数组排序是一种应该避免的反模式。数组索引永远不能保证保持相同的顺序,并且与所涉及的数据没有很强的联系。
如需EasyPost API的进一步帮助,请通过support@easypost.com联系我们的支持团队。祝您好运!

相关问题