1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159:
<?php
namespace Antavo;
use Antavo\RestClient\RestClient;
/**
* Antavo Loyalty API client class.
*/
class ApiClient extends RestClient {
/**
* {@inheritdoc}';
*/
protected $base_url = 'https://api.antavo.com/v1';
/**
* @var string Current client version
*/
protected $version = '1.1.0';
/**
* @var string Antavo Loyalty API key
*/
protected $api_key;
/**
* @var string API secret
*/
protected $secret;
/**
* Constructs client object: sets API credentials.
*
* @param string $api_key Antavo Loyalty API key.
* @param string $secret API secret.
*/
public function __construct($api_key, $secret) {
$this->api_key = $api_key;
$this->secret = $secret;
}
/**
* Automatically opts customer in if consent cookie is present.
*
* @param string $customer Unique customer ID.
* @param array $data Custom data to store with the event. See
* {@link https://docs.antavo.com/api/events/opt_in event documentation}
* for required an optional keys.
* @return mixed
*/
public function autoOptInCustomer($customer, array $data = array()) {
if (isset($_COOKIE['__alo']) && '1' == $_COOKIE['__alo']) {
$result = $this->optInCustomer($customer, $data);
setcookie('__alo', '2', 0, '/', implode('.', array_slice(explode('.', getenv('HTTP_HOST')), -2)));
$_COOKIE['__alo'] = '2';
return $result;
}
}
/**
* {@inheritdoc}
*
* It automatically appends <tt>api_key</tt> and calculated signature to
* request data.
*/
public function call($method, $url, $data = NULL, array $curl_options = array()) {
$data['api_key'] = $this->api_key;
$curl_options[CURLOPT_HTTPHEADER][] = 'User-Agent: Antavo PHP SDK API client v' . $this->version;
return parent::call($method, $url, $data, $curl_options);
}
/**
* Posts an {@link https://docs.antavo.com/api/events/opt_in opt-in event}
* to the Events API.
* <code>
* $client->optInCustomer('8eb1b522', array(
* // Providing an email address is mandatory.
* 'email' => 'john.doe@example.com'
* ));
* </code>
*
* @param string $customer Unique customer ID.
* @param array $data Custom data to store with the event. See
* {@link https://docs.antavo.com/api/events/opt_in event documentation}
* for required an optional keys.
* @return mixed
*/
public function optInCustomer($customer, array $data = array()) {
return $this->postEvent('opt_in', $customer, $data);
}
/**
* Posts an {@link https://docs.antavo.com/api/events/opt_out opt-out event}
* to the Events API.
* <code>
* $client->optOutCustomer('8eb1b522', array(
* 'reason' => 'Not interested'
* ));
* </code>
*
* @param string $customer Unique customer ID.
* @param array $data Custom data to store with the event. See
* {@link https://docs.antavo.com/api/events/opt_out event documentation}
* for required an optional keys.
* @return mixed
*/
public function optOutCustomer($customer, array $data = array()) {
return $this->postEvent('opt_out', $customer, $data);
}
/**
* Posts an event to the {@link https://docs.antavo.com/api/events Antavo
* Loyalty Events API}.
* <code>
* $customer = array(
* 'id' => '8eb1b522',
* 'first_name' => 'John',
* 'last_name' => 'Doe',
* 'email' => 'john.doe@example.com'
* );
* $client = new Antavo\ApiClient('API_KEY', 'API_SECRET');
* try {
* $result = $client->postEvent('opt_in', $customer['id'], array(
* 'first_name' => $customer['first_name'],
* 'last_name' => $customer['last_name'],
* 'email' => $customer['email']
* ));
* } catch (Antavo\RestClient\Exceptions\Exception $e) {
* echo $e->getMessage();
* }
* </code>
*
* @param string $action Action name.
* @param string $customer Unique customer ID.
* @param array $data Custom data to store with the event.
* @return mixed
*/
public function postEvent($action, $customer, array $data = array()) {
return $this->call('POST', '/events', compact('action', 'customer', 'data'));
}
/**
* {@inheritdoc}
*/
protected function processJsonResult($result) {
$parsed_result = parent::processJsonResult($result);
if (isset($parsed_result->error)) {
$this->setError(new ApiClientException(
sprintf(
'%s: %s',
$parsed_result->error->type,
$parsed_result->error->message
),
$this->last_result_info['http_code']
));
}
return $parsed_result;
}
}