Laravel
In this guide we will show you how to load RocketShipIt into a Laravel PHP project. This guide will not teach you Laravel or Laravel best practices. It only serves as a guide on how to load RocketShipIt into your project. We'll let you take it from there.
Load RocketShipIt
Within your Laravel project's directory, execute:
composer require rocketshipit/rocketshipit
This will add the RocketShipIt PHP client to your project.
If successful you should see something like this:
Using version dev-master for rocketshipit/rocketshipit
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing rocketshipit/rocketshipit (dev-master 56e3e18): Cloning 56e3e185b4 from cache
Writing lock file
Generating optimized autoload files
Package manifest generated successfully.
Test that it worked
To test that we can now use RocketShipIt in our project we are going to make a test route in routes/web.php
.
Add the following code:
Route::get('/rocketshipit-test', function () {
$rs = new \RocketShipIt\RocketShipIt();
echo 'hello world';
});
Once added you should be able to see hello world
when you navigate to: http://localhost:8000/rocketshipit-test. If you see an error instead, the package was not properly installed.
After you have confirmed that the package was installed properly we can try a request.
Change your route to the following making sure to replace the fields with YOUR in it:
Route::get('/rocketshipit-test', function () {
$rs = new \RocketShipIt\RocketShipIt();
$rs->apiKey = 'YOUR RocketShipIt API KEY HERE';
$response = $rs->request(
array(
'carrier' => 'UPS',
'action' => 'Track',
'params' =>
array(
'username' => 'YOUR UPS USERNAME',
'password' => 'YOUR UPS PASSWORD',
'key' => 'YOUR UPS API KEY',
'tracking_number' => '1Z12345E0205271688', // this is a test tracking number
'test' => true // set to false to use production UPS servers
),
));
echo '<pre>'. print_r($response, true). '</pre>';
});
If successful you should see something similar to:
...
[tracking_number] => 1Z12345E0205271688
[activity] => Array
(
[0] => Array
(
[description] => DELIVERED
[location] => Array
(
[city] => ANYTOWN
[state] => GA
[postal_code] => 30340
[country] => US
[code] => ML
[description] => BACK DOOR
...
Connect to self-hosted RocketShipIt
By setting $rs->apiKey
in the previous example we are connecting to the cloud version of Rocketshipit. If you want to self-host RocketShipIt you will need to either set the http_endpoint
to your domain/endoint or set the path to the RocketShipIt binary file.
If you want to host RocketShipIt yourself using the RocketShipIt binary you will need to copy the RocketShipIt
and license.lic
files to your Laravel project directory.
From inside your Laravel project directory:
mkdir rocketshipit
tar -xf ~/Downloads/rocketshipit2-linux-amd64.tar.gz -C rocketshipit
cp ~/Downloads/license.lic rocketshipit
Then change your route to include the path to the RocketShipIt binary:
// The RocketShipIt binary can be anywhere on your server. In this
// example it is placed in the `rocketshipit` folder.
$rs->binWorkingDir = __DIR__. '/../rocketshipit/';
$rs->binPath = $rs->binWorkingDir. 'RocketShipIt';
If you are self-hosting by running a RocketShipIt HTTP server you simply need to set the http_endpoint
option:
$rs->options['http_endpoint'] = 'http://localhost:8080/api/v1/';