Home

Address Example

Download
<?php

/*
 * Example PHP code to interact with the address book 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/address', $hostname);

// Method: Create [POST]

$data = http_build_query(array(
  'company_name' => 'API Demo',
  'contact_name' => 'John Demo',
  'address' => '1234 Demo St',
  'address_2' => 'Suite 500',
  'city' => 'Blaine',
  'state' => 'MN',
  'postal_code' => '55434',
  'phone' => '123-456-7890',
  'extension' => '',
  'is_pickup' => '2', // Pickup and delivery
));

$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]

// Retrieve the URL of the newly created address.
$content = $xml->head->meta->attributes()->content;
list($temp, $addressUrl) = explode('=', $content);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $addressUrl);
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']);

$xml = new SimpleXMLElement($html);

// Display the retrieved address.
foreach ($xml as $key => $value)
{
  echo ucwords(str_replace('_', ' ', $key)).': '.$value."\n";
}

// 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 addresses in the list.
$i = 0;
$xml = new SimpleXMLElement($html);

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

  $i++;

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


// Method: Update [PUT]

// Only supply changed fields.
$data = http_build_query(array(
  'company_name' => 'API Update Demo',
  'contact_name' => 'Bob',
  'address' => '12345 Circle St.',
  'address_2' => '',
  'city' => 'Coon Rapids',
  'postal_code' => '55448',
  'phone' => '7635551234',
  'extension' => '999',
));

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

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

$xml = new SimpleXMLElement($html);

// Display the updated address.
foreach ($xml as $key => $value)
{
  echo ucwords(str_replace('_', ' ', $key)).': '.$value."\n";
}

// Method: Delete [DELETE]

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $addressUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // Do a DELETE.
$html = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

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