Skip to content

Instantly share code, notes, and snippets.

@Ravenna
Created July 21, 2020 18:56
Show Gist options
  • Save Ravenna/4dcd8dd68fe50a25f0bd27c3d953fac5 to your computer and use it in GitHub Desktop.
Save Ravenna/4dcd8dd68fe50a25f0bd27c3d953fac5 to your computer and use it in GitHub Desktop.
public $base_url = "https://spaceangelsnetwork.api-us1.com";
public $secure_token;
public $debug = false;
function __construct($secure_token = NULL) {
$this->secure_token = config('services.active_campaign.key');
if($secure_token) $this->secure_token = $secure_token;
}
// Run HTTP Requests
function httpRequest($url, $body) {
$method = "POST";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($body) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
}
$data = curl_exec($curl);
if ($this->debug) {
echo "In httpRequest: Recieved DATA\n===========\n" . $data . "\n===========\n";
}
if(curl_error($curl))
{
\Log::info('curl error:' . curl_error($curl));
return "AC Fail";
}
curl_close($curl);
return $data;
}
//
//
// Create Contact
function addUserToAC($user){
$addUrl = $this->base_url . "/admin/api.php?api_action=contact_add";
$name = explode(" ", $user['name']);
$body = array(
'api_action' => 'contact_add',
'api_key' => $this->secure_token,
'api_output' => 'json',
'email' => $user['email'],
'first_name' => $name[0]
);
if(array_key_exists(1, $name)){
$body['last_name'] = $name[1];
}
if(array_key_exists('phone', $user)){
$body['phone'] = $user['phone'];
}
if (array_key_exists('referred_by', $user) && array_key_exists('referred_method', $user)) {
if ($user['referred_by'] == "Member") {
$body['field[%REFERRER%,0]'] = $user['referred_method'];
}
}
$response = $this->httpRequest($addUrl, $body);
$jsonResponse = json_decode($response, true);
return $jsonResponse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment