Home

Rate Example

Download
<?php

/*
 * Example PHP code to create rate requests and rate dispatches using cURL.
 *
 * This script can be run from the command line. Simply supply the hostname,
 * username, and password arguments.
 *
 * SAV Transportation Group
 */

if ($argc !== 4)
{
  echo sprintf("Usage: php %s hostname username password\n", $argv[0]);
  exit();
}

$hostname = $argv[1]; // Use vsa.savtrans.com
$username = $argv[2];
$password = $argv[3];
$url = sprintf('https://%s/api.php/rest/rate_request', $hostname);

// Rate Request

// Method: Create [POST]

$data = http_build_query(array(
  'pickup_postal' => '55448',
  'delivery_postal' => '60608',
  'add_charges' => array(4, 8), // Lift-gate and residential delivery accessorials.
  'items' => array(
    0 => array(
      'weight' => 500,
      'pallets' =>1,
      'pieces' => 1,
      'class' => 50,
      'length' => 48, // Length, width, and height are optional and in inches.
      'width' => 40,
      'height' => 36,
    ),
  ),
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, true); // Do a POST.
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "\n ***** Create [POST] ***** \n";
echo sprintf("URL: %s\n", $info['url']);
echo sprintf("HTTP Status: %s\n", $info['http_code']);

$xml = new SimpleXMLElement($html);

// Method: Retrieve [GET]

$content = $xml->head->meta->attributes()->content;
list($temp, $rateUrl) = explode('=', $content);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rateUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, false); // Do a GET.
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "\n ***** Retrieve [GET] ***** \n";
echo sprintf("URL: %s\n", $info['url']);
echo sprintf("HTTP Status: %s\n", $info['http_code']);

echo "Rate Results:\n";

$retrieveXml = new SimpleXMLElement($html);

foreach ($retrieveXml->rate_results->rate_result as $result)
{
  echo sprintf("  %s: $%s\n", $result->contract->carrier->name, $result->total_charge);
}

// Method: List [GET]

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, false); // Do a GET.
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "\n ***** List [GET] ***** \n";
echo sprintf("URL: %s\n", $info['url']);
echo sprintf("HTTP Status: %s\n", $info['http_code']);

// Display 3 rate requests in the list.
$i = 0;
$xml = new SimpleXMLElement($html);

foreach ($xml->rate_request as $request)
{
  foreach ($request as $key => $value)
  {
    echo ucwords(str_replace('_', ' ', $key)).': '.$value."\n";
  }

  $i++;

  if ($i < 3)
  {
    echo "------------------------------\n";
  }
  else
  {
    break;
  }
}

// Rate Dispatch

// Method: Create [POST]

$dCreateUrl = sprintf('https://%s/api.php/rest/rate_dispatch', $hostname);

// Get the request hash and the first contract hash.
$rHash = explode('/', $rateUrl);
$rHash = $rHash[6];
$cHash = (string) $retrieveXml->rate_results->rate_result[0]->contract->hash;

$data = http_build_query(array(
  'p_company_name' => 'Demo Company',
  'p_contact_name' => 'John Demo',
  'p_address' => '11325 Xeon St NW',
  'p_address_2' => '', // Optional
  'p_city' => 'Coon Rapids',
  'p_phone' => '123-456-7890',
  'p_extension' => '123', // Optional
  'p_date' => date('m/d/Y', strtotime('+0 weekday')), // Limited to the next 5 business days. Format: MM/DD/YYYY
  'p_time_1' => '8:00AM',
  'p_time_2' => '5:00PM',
  'd_company_name' => 'Demo Company',
  'd_contact_name' => 'John Demo',
  'd_address' => '1234 Demo St',
  'd_address_2' => '', // Optional
  'd_city' => 'Chicago',
  'd_phone' => '123-456-7890',
  'd_extension' => '123', // Optional
  'rate_request_hash' => $rHash,
  'contract_hash' => $cHash,
  'items' => array(
    0 => array(
      'description' => 'Demo Item',
    ),
  ),
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dCreateUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, true); // Do a POST.
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "\n ***** Dispatch Create [POST] ***** \n";
echo sprintf("URL: %s\n", $info['url']);
echo sprintf("HTTP Status: %s\n", $info['http_code']);

$dCreateXml = new SimpleXMLElement($html);

// Method: Retrieve [GET]

$content = $dCreateXml->head->meta->attributes()->content;
list($temp, $dispatchUrl) = explode('=', $content);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $dispatchUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_POST, false); // Do a GET.
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

echo "\n ***** Retrieve [GET] ***** \n";
echo sprintf("URL: %s\n", $info['url']);
echo sprintf("HTTP Status: %s\n", $info['http_code']);

$dRetrieveXml = new SimpleXMLElement($html);

foreach ($dRetrieveXml as $key => $value)
{
  if (strlen($value))
  {
    echo ucwords(str_replace('_', ' ', $key)).': '.$value."\n";
  }
}