2017-04-03 53 views
0

我目前已經有EasyPost創建貨件。當我創建貨物時,它爲我提供了所有可用的服務和費率。獲取特定服務的費率 - EasyPost

我想動態選擇服務,只返回該速率。

我最初會通過使用索引號讀取數組(我猜如何描述它)。

這樣做的問題是每次創建新貨件時,費率數組順序都會發生變化,因此將顯示$ shipment-> rates [0] ['rate'],然後下一次首先類。

我想用「Express」創建一個貨件並且只返回那個價格。

任何意見,將不勝感激。

這裏是我的代碼:

$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>'; 

回答

0

在EasyPost的Getting Started Guide有購買特定載體+率的一個例子:

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

我不想買它,直到我知道是什麼率是。 – foxtangocharlie

+0

在這種情況下,你只需要做一些數組過濾。除了選擇運營商(您可以使用carrier_accounts字段),這些詳細信息是業務邏輯,EasyPost有意避開其便捷方法,以便您確切地確定您想要的內容。見http://php.net/manual/en/function.array-filter.php – KFunk

相關問題