PHP code example of tigusigalpa / shippo-php
1. Go to this page and download the library: Download tigusigalpa/shippo-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tigusigalpa / shippo-php example snippets
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory;
use Tigusigalpa\Shippo\Shippo;
use Tigusigalpa\Shippo\Config;
$httpFactory = new HttpFactory();
$config = Config::make('your_api_token');
$shippo = new Shippo(
httpClient: new GuzzleClient(),
requestFactory: $httpFactory,
streamFactory: $httpFactory,
config: $config
);
// Create an address
$address = $shippo->addresses()->create([
'name' => 'John Doe',
'street1' => '215 Clayton St.',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94117',
'country' => 'US',
'phone' => '+1 555 341 9393',
'email' => '[email protected] ',
]);
echo $address->objectId;
use Tigusigalpa\Shippo\Laravel\Facades\Shippo;
// Create a shipment and get rates
$shipment = Shippo::shipments()->create([
'address_from' => [
'name' => 'Sender Name',
'street1' => '123 Main St',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94103',
'country' => 'US',
],
'address_to' => [
'name' => 'Recipient Name',
'street1' => '456 Oak Ave',
'city' => 'Los Angeles',
'state' => 'CA',
'zip' => '90001',
'country' => 'US',
],
'parcels' => [
[
'length' => '5',
'width' => '5',
'height' => '5',
'distance_unit' => 'in',
'weight' => '2',
'mass_unit' => 'lb',
],
],
]);
// Get available rates
foreach ($shipment->rates as $rate) {
echo "{$rate['provider']} - {$rate['servicelevel_name']}: \${$rate['amount']}\n";
}
$validatedAddress = $shippo->addresses()->validate([
'name' => 'John Doe',
'street1' => '215 Clayton St.',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94117',
'country' => 'US',
]);
if ($validatedAddress->validationStatus === ValidationStatus::VALID) {
echo "Address is valid!";
}
// 1. Create a shipment
$shipment = $shippo->shipments()->create([
'address_from' => [
'name' => 'Your Store',
'street1' => '123 Business St',
'city' => 'San Francisco',
'state' => 'CA',
'zip' => '94103',
'country' => 'US',
],
'address_to' => [
'name' => 'Customer Name',
'street1' => '456 Customer Ave',
'city' => 'Los Angeles',
'state' => 'CA',
'zip' => '90001',
'country' => 'US',
],
'parcels' => [
[
'length' => '10',
'width' => '8',
'height' => '4',
'distance_unit' => 'in',
'weight' => '5',
'mass_unit' => 'lb',
],
],
]);
// 2. Select a rate (e.g., the cheapest one)
$rateId = $shipment->rates[0]['object_id'];
// 3. Purchase the label
$transaction = $shippo->transactions()->create([
'rate' => $rateId,
'label_file_type' => 'PDF',
'async' => false,
]);
// 4. Get the label URL
echo "Label URL: {$transaction->labelUrl}\n";
echo "Tracking Number: {$transaction->trackingNumber}\n";
$tracking = $shippo->tracking()->retrieve('usps', '9205590164917312751089');
echo "Status: {$tracking->trackingStatus}\n";
echo "ETA: {$tracking->eta}\n";
foreach ($tracking->trackingHistory as $event) {
echo "{$event['status_date']} - {$event['status_details']}\n";
}
// Create customs items
$customsItem = $shippo->customs()->createItem([
'description' => 'T-Shirt',
'quantity' => 2,
'net_weight' => '0.5',
'mass_unit' => 'lb',
'value_amount' => '20.00',
'value_currency' => 'USD',
'origin_country' => 'US',
]);
// Create customs declaration
$customsDeclaration = $shippo->customs()->createDeclaration([
'contents_type' => 'MERCHANDISE',
'contents_explanation' => 'T-Shirts',
'non_delivery_option' => 'RETURN',
'certify' => true,
'certify_signer' => 'John Doe',
'items' => [$customsItem['object_id']],
]);
// Create shipment with customs
$shipment = $shippo->shipments()->create([
'address_from' => [...],
'address_to' => [...],
'parcels' => [...],
'customs_declaration' => $customsDeclaration['object_id'],
]);
$addresses = $shippo->addresses()->list([
'results' => 25,
'page' => 1,
]);
foreach ($addresses as $address) {
echo "{$address->name} - {$address->city}, {$address->state}\n";
}
if ($addresses->hasMorePages()) {
echo "More addresses available!";
}
$refund = $shippo->refunds()->create('transaction_id_here');
echo "Refund status: {$refund['status']}\n";
// Create a batch
$batch = $shippo->batches()->create([
'default_carrier_account' => 'carrier_account_id',
'default_servicelevel_token' => 'usps_priority',
'label_filetype' => 'PDF_4x6',
]);
// Add shipments to batch
$shippo->batches()->addShipments($batch['object_id'], [
'shipment_id_1',
'shipment_id_2',
'shipment_id_3',
]);
// Purchase all labels in batch
$result = $shippo->batches()->purchase($batch['object_id']);
use Tigusigalpa\Shippo\Exceptions\AuthenticationException;
use Tigusigalpa\Shippo\Exceptions\RateLimitException;
use Tigusigalpa\Shippo\Exceptions\ValidationException;
use Tigusigalpa\Shippo\Exceptions\NotFoundException;
use Tigusigalpa\Shippo\Exceptions\ServerException;
use Tigusigalpa\Shippo\Exceptions\ShippoException;
try {
$address = $shippo->addresses()->create([...]);
} catch (AuthenticationException $e) {
// Invalid API token
echo "Authentication failed: {$e->getMessage()}";
} catch (ValidationException $e) {
// Invalid data provided
echo "Validation error: {$e->getMessage()}";
print_r($e->getResponse());
} catch (RateLimitException $e) {
// Rate limit exceeded (automatically retried)
echo "Rate limited. Retry after: {$e->getRetryAfter()} seconds";
} catch (NotFoundException $e) {
// Resource not found
echo "Not found: {$e->getMessage()}";
} catch (ServerException $e) {
// Shippo server error
echo "Server error: {$e->getMessage()}";
} catch (ShippoException $e) {
// Any other Shippo error
echo "Error: {$e->getMessage()}";
}
$config = Config::make('your_api_token', [
'api_version' => '2018-02-08',
'base_url' => 'https://api.goshippo.com',
'is_test' => true,
'timeout' => 30,
'retry_attempts' => 3,
'retry_delay' => 1000, // milliseconds
]);
return [
'api_token' => env('SHIPPO_API_TOKEN'),
'api_version' => env('SHIPPO_API_VERSION', '2018-02-08'),
'base_url' => env('SHIPPO_BASE_URL', 'https://api.goshippo.com'),
'is_test' => env('SHIPPO_IS_TEST', false),
'timeout' => env('SHIPPO_TIMEOUT', 30),
'retry_attempts' => env('SHIPPO_RETRY_ATTEMPTS', 3),
'retry_delay' => env('SHIPPO_RETRY_DELAY', 1000),
];
bash
composer bash
php artisan vendor:publish --tag=shippo-config
bash
composer analyse