Example: PHP

Last modified on September 11, 2023 at 5:50 pm

<?php
function getSignature($stringToSign, $key) {
  $binary_hmac = pack(“H40”, hash_hmac('sha1', trim($stringToSign), $key));
  return base64_encode($binary_hmac);
}

$hostname = '[your virtual host].digitalchalk.com'; 
$service_name = 'v4';
$operation_name = 'doesUserExist';

// Get these 2 values from DigitalChalk 
$public_key = '[your DC public key for API]'; 
$secret_key = '[your DC secret key for API]';

$timestamp = gmdate('c'); // RFC 8601 date format 
// Fix for some PHP's that use +00:00 for GMT 
$timestamp = str_replace('+00:00', 'Z', $timestamp);

$to_sign = $service_name . $operation_name . $timestamp; 
$signature = getSignature($to_sign, $secret_key);

echo 'To Sign is now ' . $to_sign . '<br/>';
echo 'Timestamp is now ' . $timestamp . '<br/>'; 
echo 'Signature is now ' . $signature . '<br/>';

$auth = array(
  'accessKey' => $public_key, 
  'timestamp' => $timestamp, 
  'signature' => $signature
);

$wsdl_opts = array(
  'ssl' => array( 'allow_self_signed' => true ), 
  'trace' => 1,
  'exceptions' => 1
);

$wsdl_url = 'https://' . $hostname . '/dc/api/v4.wsdl';

echo 'Using wsdl url of <a href=”' . $wsdl_url . '”>' . $wsdl_url . '</a></br>'; 
echo 'Calling operation “<b>' . $operation_name . '</b>”</br>';

$client = new SoapClient($wsdl_url, $wsdl_opts); 

try {
  
  $result = $client->doesUserExist(array('username' => '[username to check]', 'auth' => $auth));

  # If the call had been getAvailableOfferings, it might have looked like this: 
  # $result = $client->getAvailableOfferings(array('auth' => $auth));

  echo '<br/>RESULT</br>';

  var_dump($result);

} catch(SoapFault $soapFault) { 
  
  echo '<br/>FAULT<br/>';
  
  var_dump($soapFault);
  
  $lastRequest = $client->__getLastRequest(); 
  
  echo '<br/>LAST REQUEST</br>';
  echo htmlentities($lastRequest);
  echo '<br/>LAST RESPONSE</br>';
  echo htmlentities($client->__getLastResponse());
  
}

echo '<br/>FUNCTIONS</br>'; 
var_dump($client->__getFunctions());
?>