Created
February 6, 2017 08:52
-
-
Save DmitriySmirnov/8d65a1417d9ffdfc9efe762fa492bbcc to your computer and use it in GitHub Desktop.
Create contact with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Wild Apricot API helper class | |
class WaApiClient { | |
const AUTH_URL = 'https://oauth.wildapricot.org/auth/token'; | |
private $tokenScope = 'auto'; | |
private static $_instance; | |
private $token; | |
public function initTokenByContactCredentials($userName, $password, $scope = null) { | |
if ($scope) { | |
$this->tokenScope = $scope; | |
} | |
$this->token = $this->getAuthTokenByAdminCredentials($userName, $password); | |
if (!$this->token) { | |
throw new Exception('Unable to get authorization token.'); | |
} | |
} | |
public function initTokenByApiKey($apiKey, $scope = null) { | |
if ($scope) { | |
$this->tokenScope = $scope; | |
} | |
$this->token = $this->getAuthTokenByApiKey($apiKey); | |
if (!$this->token) { | |
throw new Exception('Unable to get authorization token.'); | |
} | |
} | |
// this function makes authenticated request to API | |
// ----------------------- | |
// $url is an absolute URL | |
// $verb is an optional parameter. | |
// Use 'GET' to retrieve data, | |
// 'POST' to create new record | |
// 'PUT' to update existing record | |
// 'DELETE' to remove record | |
// $data is an optional parameter - data to sent to server. Pass this parameter with 'POST' or 'PUT' requests. | |
// ------------------------ | |
// returns object decoded from response json | |
public function makeRequest($url, $verb = 'GET', $data = null) { | |
if (!$this->token) { | |
throw new Exception('Access token is not initialized. Call initTokenByApiKey or initTokenByContactCredentials before performing requests.'); | |
} | |
$ch = curl_init(); | |
$headers = array( | |
'Authorization: Bearer ' . $this->token, | |
'Content-Type: application/json' | |
); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
if ($data) { | |
$jsonData = json_encode($data); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); | |
$headers = array_merge($headers, array('Content-Length: '.strlen($jsonData))); | |
} | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$jsonResult = curl_exec($ch); | |
if ($jsonResult === false) { | |
throw new Exception(curl_errno($ch) . ': ' . curl_error($ch)); | |
} | |
// var_dump($jsonResult); // Uncomment line to debug response | |
curl_close($ch); | |
return json_decode($jsonResult, true); | |
} | |
private function getAuthTokenByAdminCredentials($login, $password) { | |
if ($login == '') { | |
throw new Exception('login is empty'); | |
} | |
$data = sprintf("grant_type=%s&username=%s&password=%s&scope=%s", 'password', urlencode($login), urlencode($password), urlencode($this->tokenScope)); | |
throw new Exception('Change clientId and clientSecret to values specific for your authorized application. For details see: https://help.wildapricot.com/display/DOC/Authorizing+external+applications'); | |
$clientId = 'SamplePhpApplication'; | |
$clientSecret = 'open_wa_api_client'; | |
$authorizationHeader = "Authorization: Basic " . base64_encode( $clientId . ":" . $clientSecret); | |
return $this->getAuthToken($data, $authorizationHeader); | |
} | |
private function getAuthTokenByApiKey($apiKey) { | |
$data = sprintf("grant_type=%s&scope=%s", 'client_credentials', $this->tokenScope); | |
$authorizationHeader = "Authorization: Basic " . base64_encode("APIKEY:" . $apiKey); | |
return $this->getAuthToken($data, $authorizationHeader); | |
} | |
private function getAuthToken($data, $authorizationHeader) { | |
$ch = curl_init(); | |
$headers = array( | |
$authorizationHeader, | |
'Content-Length: ' . strlen($data) | |
); | |
curl_setopt($ch, CURLOPT_URL, WaApiClient::AUTH_URL); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$response = curl_exec($ch); | |
if ($response === false) { | |
throw new Exception(curl_errno($ch) . ': ' . curl_error($ch)); | |
} | |
// var_dump($response); // Uncomment line to debug response | |
$result = json_decode($response , true); | |
curl_close($ch); | |
return $result['access_token']; | |
} | |
public static function getInstance() { | |
if (!is_object(self::$_instance)) { | |
self::$_instance = new self(); | |
} | |
return self::$_instance; | |
} | |
public final function __clone() { | |
throw new Exception('It\'s impossible to clone singleton "' . __CLASS__ . '"!'); | |
} | |
private function __construct() { | |
if (!extension_loaded('curl')) { | |
throw new Exception('cURL library is not loaded'); | |
} | |
} | |
public function __destruct() { | |
$this->token = null; | |
} | |
} | |
function generateRandomString($length=32) { | |
return substr(md5(uniqid(mt_rand(), true)), 0, $length); | |
} | |
function getWaAccountDetails () { | |
global $waApiClient; | |
$url = 'https://api.wildapricot.org/v2/Accounts/'; | |
$response = $waApiClient->makeRequest($url); | |
return $response[0]; // usually you have access to one account | |
} | |
function createWaContact($postFields) { | |
global $waApiClient; | |
global $waAccountUrl; | |
$url = $waAccountUrl . '/Contacts/'; | |
$data = array( | |
'FirstName' => $postFields['firstName'], | |
'LastName' => $postFields['lastName'], | |
'Password' => $postFields['password'], | |
'Email' => $postFields['email'], | |
'MembershipLevel' => array( | |
'Id' => 838800 | |
), | |
'MembershipEnabled' => true, | |
'Status' => 'Active', | |
'FieldValues' => array( | |
array( | |
'SystemCode' => 'RenewalDue', | |
'Value' => $postFields['date'] | |
) | |
) | |
); | |
return $waApiClient->makeRequest($url, 'POST', $data); | |
} | |
$password = generateRandomString(10); | |
$date = date("Y-m-d", strtotime('+1 year')); | |
//Wild Apricot Connection | |
$waApiClient = WaApiClient::getInstance(); | |
$waApiKey = "rfolnz5z17dxzbt47pejw0zfr4qn11"; | |
$waApiClient->initTokenByA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment