phpshippo

I am trying to create a test label from Shippo, however I get a rate error


However I get this error, that I don't know what it means:

Fatal error: Uncaught Shippo_InvalidRequestError: {"rate": ["This field is required."]} in /home/****/shippo/lib/Shippo/ApiRequestor.php:100 Stack trace: #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('{"rate": ["This...', 400) #2 /home/****/shippo/lib/Shippo/ApiResource.php(107): Shippo_ApiRequestor->request('post', '/v1/transaction...', Array) #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): Shippo_Transaction::create(Array) #5 {main} thrown in /home/****/shippo/lib/Shippo/ApiRequestor.php on line 100

I had gotten a similar message about the parcel field too, so I changed in the PHP script parcels to parcel. This is the example code from https://github.com/goshippo/shippo-php-client/blob/master/examples/basic-shipment.php

My Code:

<?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/
?>

Solution

  • Although I am not 100% certain here, you are likely using the latest version of the PHP wrapper but do not have the latest version of the Shippo API.

    I would recommend going to the Shippo changelog and review the changes there and decide whether you would like to upgrade or not.

    The likely reason that your code is failing there, is that there (if you're on an older version of the API) you are trying to retrieve a value from a field that does not exist. You should be looking for $rate = $shipment['rates_list'][0].

    You'll see that this field was changed in the most recent version of the API.