Create a Shipping Label

After you have signed up for a carrier developer key and added your credentials via the RocketShipIt online form or manually added them in the script it’s time to make your first label.

Make sure you have loaded RocketShipIt and try one of the following examples.

Carriers:

UPS

Basic single package example:

<?php
$shipment = new \RocketShipIt\Shipment('UPS');

$shipment->setParameter('toCompany', 'John Doe');
$shipment->setParameter('toPhone', '1231231234');
$shipment->setParameter('toAddr1', '101 W Main');
$shipment->setParameter('toCity', 'Bozeman');
$shipment->setParameter('toState', 'MT');
$shipment->setParameter('toCode', '59715');

$package = new \RocketShipIt\Package('UPS');
$package->setParameter('length', '5');
$package->setParameter('width', '5');
$package->setParameter('height', '5');
$package->setParameter('weight', '5');

$shipment->addPackageToShipment($package);

$response = $shipment->submitShipment();

Note

RocketShipIt for UPS™ is also capable of generating a shipment with multiple packages. See: Creating a Shipment with Multiple Packages.

Note

UPS Requires package dimensions but they can be set to a constant value in most cases. See: Length, Width, and Height.

The line of the code will output an array with the results. This will include a shipment tracking number, package tracking numbers, charges, and a label in the specified format.

Here is an example response:

Array
(
  [charges] => 6.89
  [trk_main] => 1Z5977460394839629
  [pkgs] => Array
    (
      [0] => Array
        (
        [pkg_trk_num] => 1Z5977460394839629
        [label_fmt] => ZPL
        [label_img] => Cl5YQV5MUk5eTU5ZXk1GTixOXkxIMTUsMTBPSV5QVzg...

Images are sent in the Base64 format (binary to text encoding) and can be decoded and saved as a file with the base64_decode function. Or, alternatively you can display images directly embedded in html with this format if you set the imageType parameter to GIF:

<img src="data:image/gif;base64,iVBORw0KGgoAAAANS..." />

FedEx

After you have signed up for a FedEx developer key and added your credentials via the RocketShipIt fedex settings form or manually added them in the script it’s time to make your first label.

Make sure you have loaded RocketShipIt and try the following:

<?php
$shipment = new \RocketShipIt\Shipment('fedex');

$shipment->setParameter('toCompany', 'John Doe');
$shipment->setParameter('toName', 'John Doe');
$shipment->setParameter('toPhone', '1231231234');
$shipment->setParameter('toAddr1', '111 W Legion');
$shipment->setParameter('toCity', 'Whitehall');
$shipment->setParameter('toState', 'MT');
$shipment->setParameter('toCode', '59759');

$shipment->setParameter('length', '5');
$shipment->setParameter('width', '5');
$shipment->setParameter('height', '5');
$shipment->setParameter('weight','5');

$response = $shipment->submitShipment();

if (isset($response['error']) && $response['error'] != '') {
    // Something went wrong, show debug information
    echo $shipment->debug();
} else {
    print_r($response); // display response
    // Create label as a file
    file_put_contents('label.pdf', base64_decode($response['pkgs'][0]['label_img']));
}

USPS

After you have signed up for a Stamps.com account and added your credentials via the RocketShipIt usps settings form or manually added them in the script it’s time to make your first label.

USPS is a little different than UPS/FedEx, instead of creating a shipment with the package dimensions and weight you create a rate request and then pass the rate response back as part of the shipment request.

One key thing to keep in mind is that the rate that comes back also contains all the possible addons that are available.

Note

Tracking numbers will not show up when you are creating test labels with debugMode set to 1. Only production labels will have a tracking number. The variable when returned will be called TrackingNumber.

Here is an example creating a shipment with the delivery confirmation addon:

<?php
$shipment = new \RocketShipIt\Shipment('STAMPS');
$shipment->setParameter('toCompany', 'RocketShipIt');
$shipment->setParameter('toName', 'John Doe');
$shipment->setParameter('toAddr1', '111 W Legion');
$shipment->setParameter('toCity', 'Whitehall');
$shipment->setParameter('toState', 'MT');
$shipment->setParameter('toCode', '59759');
$shipment->setParameter('referenceValue', '123adsf');

$rate = new \RocketShipIt\Rate('STAMPS');
$rate->setParameter('toCode','59759');
$rate->setParameter('weight','5');
$response = $rate->getAllRates();

$rates = $response->Rates;
$rate = $rates->Rate;

# Select the rate/service you want
# from a list of all available
$package = $rate[3];

# Remove all addons
$package->AddOns = null;

# Add the addons you want for this
# shipment
$addons = array();
$a = new \stdClass();
$a->AddOnType = 'US-A-DC';
array_push($addons, $a);
$package->AddOns = $addons;
//print_r($package);

// The rate can suggest a new zipcode
// Set this new zipcode on the shipment to avoid:
// "Rate ToZIPCode and Destination Address ZIPCode field must match."
$shipment->setParameter('toCode', $package->ToZIPCode);

$shipment->addPackageToShipment($package);

$response = $shipment->submitShipment();

Note

For a list of valid USPS addons See: Working With Addons.

You need postage available in your account in order to produce live labels.

You can get account balance and other info by doing:

<?php
$stamps = new \RocketShipIt\Carrier\Stamps();
$response = $stamps->getAccountInfo();

You can purchase more postage with your Stamps.com default payment method by doing:

<?php
$stamps = new \RocketShipIt\Carrier\Stamps();
$response = $stamps->purchasePostage(10); // $10.00 worth of postage

Note

purchasePostage() cannot be accessed from outside the US due to Stamps.com restrictions.

DHL

Make sure you have loaded RocketShipIt and try the following.

Example:

<?php
$shipment = new \RocketShipIt\Shipment('DHL');

$shipment->setParameter('shipper', 'RocketShipIt');
$shipment->setParameter('shipAddr1', '1 Zoo Rd');
$shipment->setParameter('shipCity', 'San Francisco');
$shipment->setParameter('shipState', 'California');
$shipment->setParameter('shipCode', '94132');
$shipment->setParameter('shipCountry', 'US');

$shipment->setParameter('toCompany', 'San Diego Zoo');
$shipment->setParameter('toAddr1', '2920 Zoo Dr');
$shipment->setParameter('toCity', 'San Diego');
$shipment->setParameter('toState', 'California');
$shipment->setParameter('toCode', '92101');
$shipment->setParameter('toCountry', 'US');

$shipment->setParameter('toName', 'Mark Sanborn');
$shipment->setParameter('toPhone', '1234567');

$shipment->setParameter('service', 'D');
$shipment->setParameter('weight', '5');

$response = $shipment->submitShipment();

Note

For a list of valid DHL services See: Shipment Parameters.

Canada Post

Make sure you have loaded RocketShipIt and try the following.

Example:

<?php
$shipment = new \RocketShipIt\Shipment('CANADA');
$shipment->setParameter('shipCountry', 'CA');
$shipment->setParameter('shipState', 'ON');
$shipment->setParameter('shipCity', 'Ottawa');
$shipment->setParameter('shipCode', 'K1A0B1');
$shipment->setParameter('toName', 'Mark Sanborn');
$shipment->setParameter('toAddr1', '361A Old Finch Avenue');
$shipment->setParameter('toCity', 'Toronto');
$shipment->setParameter('toState', 'ON');
$shipment->setParameter('toCode', 'M1B5K7');
$shipment->setParameter('toCountry', 'CA');
$shipment->setParameter('weight', '5');
$shipment->setParameter('length', '5');
$shipment->setParameter('width', '5');
$shipment->setParameter('height', '5');
$response = $shipment->submitShipment();

Non-Package Items

Here is an example of creating a label for a UPS Letter:

<?php
$shipment = new \RocketShipIt\Shipment('UPS');

$shipment->setParameter('toCompany', 'John Doe');
$shipment->setParameter('toPhone', '1231231234');
$shipment->setParameter('toAddr1', '111 W Legion');
$shipment->setParameter('toCity', 'Whitehall');
$shipment->setParameter('toState', 'MT');
$shipment->setParameter('toCode', '59759');
$shipment->setParameter('service', '01');

$package = new \RocketShipIt\Package('UPS');
$package->setParameter('weight','1');
$package->setParameter('packagingType', '01');

$shipment->addPackageToShipment($package);

$response = $shipment->submitShipment();

Royal Mail

Example:

<?php
$shipment = new \RocketShipIt\Shipment('royalmail');
$shipment->setParameter('toName', 'John Doe');
$shipment->setParameter('toCompany', 'John Doe');
$shipment->setParameter('toAddr1', '44-46 Morningside Road');
$shipment->setParameter('toCity', 'Edinburgh');
$shipment->setParameter('toCode', 'EH10 4BF');

$shipment->setParameter('service', 'SD1');
$shipment->setParameter('serviceType', 'D');

$package = new \RocketShipIt\Package('royalmail');
$package->setParameter('weight', '5');
$shipment->addPackageToShipment($package);

$response = $shipment->submitShipment();

OnTrac

Example:

<?php
$shipment = new \RocketShipIt\Shipment('ontrac');
$shipment->setParameter('shipState', 'CA');
$shipment->setParameter('shipCode', '94608');

$shipment->setParameter('toName', 'John Doe');
$shipment->setParameter('toPhone', '1231231234');
$shipment->setParameter('toAddr1', '101 W Main');
$shipment->setParameter('toCity', 'Emeryville');
$shipment->setParameter('toState', 'CA');
$shipment->setParameter('toCode', '94608');

// 1 – pdf
// 6 – 4 x 5 EPL label
// 7 – 4 x 5 ZPL
$shipment->setParameter('imageType', '6');

$package = new \RocketShipIt\Package('ontrac');
$package->setParameter('length', '5');
$package->setParameter('width', '5');
$package->setParameter('height', '5');
$package->setParameter('weight', '15');
$shipment->addPackageToShipment($package);

$response = $shipment->submitShipment();

Purolator

Example:

<?php
$shipment = new \RocketShipIt\Shipment('purolator');
$shipment->setParameter('shipCountry', 'CA');
$shipment->setParameter('shipState', 'BC');
$shipment->setParameter('shipCity', 'Aldergrove');
$shipment->setParameter('shipCode', 'V4W 1N7');

$shipment->setParameter('toName', 'John Doe');
$shipment->setParameter('toPhone', '1231231234');
$shipment->setParameter('toAddr1', '150 County Rd 19');
$shipment->setParameter('toCity', 'Wendover');
$shipment->setParameter('toState', 'ON');
$shipment->setParameter('toCode', 'K0A 3K0');
$shipment->setParameter('toCountry', 'CA');
$shipment->setParameter('weight', '5');

$shipment->setParameter('imageType', 'PDF');

$response = $shipment->submitShipment();