我正在尝试从Shippo创建一个测试标签,但是我得到了一个速率错误

时间:2017-05-29 22:33:25

标签: php shippo

但是我收到了这个错误,我不知道这意味着什么:

  

致命错误:未捕获的Shippo_InvalidRequestError:{" rate":["此字段为必填项。"]}位于/ home / **** / shippo / lib / Shippo / ApiRequestor.php:100堆栈跟踪:#0 /home/****/shippo/lib/Shippo/ApiRequestor.php(154):Shippo_ApiRequestor-> handleApiError(' {" rate" :[" This ...',400,Array)#1 /home/****/shippo/lib/Shippo/ApiRequestor.php(59):Shippo_ApiRequestor-> _interpretResponse(&# 39; {" rate":[" This ...',400)#2 /home/****/shippo/lib/Shippo/ApiResource.php(107) :Shippo_ApiRequestor->请求('发布',' / v1 / transaction ...',数组)#3 / home / **** / shippo / lib / Shippo / Transaction.php(14):Shippo_ApiResource :: _ scopedCreate(' Shippo_Transact ......',Array,NULL)#4 /home/****/public_html/.ship/print/done/index。 php(65):在第100行的/home/****/shippo/lib/Shippo/ApiRequestor.php中抛出的Shippo_Transaction :: create(Array)#5 {main}

我也收到了关于parcel字段的类似消息,所以我将PHP脚本parcel更改为parcel。这是https://github.com/goshippo/shippo-php-client/blob/master/examples/basic-shipment.php

的示例代码

我的代码:

<?php
require_once('****' . 'lib/Shippo.php');

Shippo::setApiKey('****');

// Example from_address array
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$from_address = array(
    'name' => 'Mr Hippo',
    'company' => 'Shippo',
    'street1' => '215 Clayton St.',
    'city' => 'San Francisco',
    'state' => 'CA',
    'zip' => '94117',
    'country' => 'US',
    'phone' => '+1 555 341 9393',
    'email' => 'mr-hippo@goshipppo.com',
);

// Example to_address array
// The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
$to_address = array(
    'name' => 'Ms Hippo',
    'company' => 'San Diego Zoo',
    'street1' => '2920 Zoo Drive',
    'city' => 'San Diego',
    'state' => 'CA',
    'zip' => '92101',
    'country' => 'US',
    'phone' => '+1 555 341 9393',
    'email' => 'ms-hippo@goshipppo.com',
);

// Parcel information array
// The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels
$parcel = array(
    'length'=> '5',
    'width'=> '5',
    'height'=> '5',
    'distance_unit'=> 'in',
    'weight'=> '2',
    'mass_unit'=> 'lb',
);

// Example shipment object
// For complete reference to the shipment object: https://goshippo.com/docs/reference#shipments
// This object has async=false, indicating that the function will wait until all rates are generated before it returns.
// By default, Shippo handles responses asynchronously. However this will be depreciated soon. Learn more: https://goshippo.com/docs/async
$shipment = Shippo_Shipment::create(
array(
    'address_from'=> $from_address,
    'address_to'=> $to_address,
    'parcel'=> array($parcel),
    'async'=> false,
));

// Rates are stored in the `rates` array
// The details on the returned object are here: https://goshippo.com/docs/reference#rates
// Get the first rate in the rates results for demo purposes.
$rate = $shipment['rates'][0];

// Purchase the desired rate with a transaction request
// Set async=false, indicating that the function will wait until the carrier returns a shipping label before it returns
$transaction = Shippo_Transaction::create(array(
    'rate'=> $rate['object_id'],
    'async'=> false,
));

// Print the shipping label from label_url
// Get the tracking number from tracking_number
if ($transaction['status'] == 'SUCCESS'){
    echo "--> " . "Shipping label url: " . $transaction['label_url'] . "\n";
    echo "--> " . "Shipping tracking number: " . $transaction['tracking_number'] . "\n";
} else {
    echo "Transaction failed with messages:" . "\n";
    foreach ($transaction['messages'] as $message) {
        echo "--> " . $message . "\n";
    }
}
// For more tutorals of address validation, tracking, returns, refunds, and other functionality, check out our
// complete documentation: https://goshippo.com/docs/
?>

2 个答案:

答案 0 :(得分:0)

虽然我不是100%肯定,但您可能使用最新版本的PHP包装器,但没有最新版本的Shippo API。

我建议您去Shippo changelog并查看其中的更改并决定是否要升级。

您的代码失败的可能原因是(如果您使用的是旧版本的API),您尝试从不存在的字段中检索值。你应该寻找$rate = $shipment['rates_list'][0]

您会在最新版本的API中看到此字段已更改。

答案 1 :(得分:0)

我遇到了这个问题。我最终引用了一个不同的示例/filter-by-delivery-time.php-那里的区别显示了调用率

$transaction = Shippo_Transaction::create(array(
    'rate'=> $eligible_rates[0]['object_id'],
    'async'=> false,
));

但是向我揭示的是错误消息不正确,实际上我的地址格式不正确。不确定为什么要这么做