Initial commit

This commit is contained in:
Will Bradley 2014-09-03 09:37:26 -07:00
commit 65d05ece68
5 changed files with 277 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
index.html
config.php

15
README.md Normal file
View File

@ -0,0 +1,15 @@
AffinityLive API (PHP)
===================
This is an extremely basic implementation of the AffinityLive API to create new contacts via a webpage contact form.
Prerequisites
-------------
- CURL, including CURL for PHP
- PHP
Installation
------------
- Copy config.php.example and index.html.example to config.php and index.html respectively. Modify to suit your needs (you'll need API credentials from AffinityLive for config.php)
- Enjoy! Please contribute improvements back to the public so we can all share and benefit. Pull requests accepted.

215
affinitylive.php Normal file
View File

@ -0,0 +1,215 @@
<?php
require("config.php"); // for clientID, clientSecret, clientURL
?>
<?php
function authorize($clientURL, $clientID, $clientSecret)
{
$url = $clientURL."/oauth2/v0/token";
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array("response_type" => 'code',
"grant_type" => 'client_credentials',
"client_id" => $clientID,
"client_secret" => $clientSecret )
));
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
if(@$response->error=='invalid_client')
{
$response = 'false';
}
return $response;
}
function createContact($clientURL, $token, $firstname, $lastname, $email, $phone, $selection, $companyID)
{
$contacturl = $clientURL."/api/v0/contacts";
$username = randomWord();
$password = randomWord();
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $contacturl,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array("company_id" => $companyID,
"_bearer_token" => $token,
"firstname" => $firstname,
"surname" => $lastname,
"email" => $email,
"phone" => $phone,
"comments" => $selection,
"username" => $username,
"password" => $password )
));
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
if(@$response->meta->status != 'ok')
{
$response = 'false';
}
return $response;
}
function getContact($clientURL, $token, $contactID)
{
$contacturl = $clientURL."/api/v0/contacts/".$contactID."?_bearer_token=".$token."&_fields=default_affiliation";
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $contacturl
));
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
if(@$response->meta->status != 'ok')
{
$response = 'false';
}
return $response;
}
function createProspect($clientURL, $token, $affiliation_id, $companyname)
{
$companyurl = $clientURL."/api/v0/prospects/";
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $companyurl,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
"_bearer_token" => $token,
"affiliation_id" => $affiliation_id,
"title" => $companyname)
));
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
return $response;
}
function getProspects($clientURL, $token)
{
$companyurl = $clientURL."/api/v0/prospects/?_bearer_token=".$token;
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $companyurl)
);
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
return $response;
}
function createCompany($clientURL, $token, $companyname)
{
$companyurl = $clientURL."/api/v0/companies";
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $companyurl,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
"_bearer_token" => $token,
"standing" => "interested",
"name" => $companyname)
));
$resp = curl_exec($curl);
curl_close($curl);
$response = json_decode($resp);
return $response;
}
function randomWord() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}
?>
<?php
if($_POST)
{
$result = authorize($clientURL, $clientID, $clientSecret);
$firstname = $_POST['contact_firstname'];
$lastname = $_POST['contact_surname'];
$email = $_POST['email'];
$phone = $_POST['phone_number'];
$companyname = $_POST['company_name'];
if(!empty($_POST['selection'])){
$selection = implode("; ", $_POST['selection']);
}
$companyresult = createCompany($clientURL, $result->access_token, $companyname);
$companyID = $companyresult->response->id;
$contactresult = createContact($clientURL, $result->access_token, $firstname, $lastname, $email, $phone, $selection, $companyID);
// var_dump($contactresult);
if( $contactresult->meta->status == "ok"){
$fullcontactresult = getContact($clientURL, $result->access_token, $contactresult->response->id);
//var_dump($fullcontactresult);
if( $fullcontactresult->meta->status == "ok" &&
$fullcontactresult->response->default_affiliation != null ){
// temporary while we figure out prospects
$message = "Thank you for contacting us! We will be in touch with you shortly.";
// get
//$prospectresult = getProspects($clientURL, $result->access_token);
// post
//$prospectresult = createProspect($clientURL, $result->access_token, $fullcontactresult->response->default_affiliation, $companyname);
// var_dump($prospectresult);
// if( $prospectresult->meta->status == "ok" ){
// $message = "Thank you for contacting us! We will be in touch with you shortly.";
// }
}
}
if( $message == null ) {
$message = "We were unable to send this form. Please contact us via phone or email.";
}
// if( !defined($prospectresult) ) {
// $message = "We were unable to send this form. Please contact us via phone or email.";
// }
echo $message;
}

7
config.php.example Normal file
View File

@ -0,0 +1,7 @@
<?php
// your configuration values from AffinityLive API
$clientID = "YOUR_ID_HERE@YOUR_DOMAIN_HERE.affinitylive.com";
$clientSecret = "YOUR_SECRET_HERE";
$clientURL = "https://YOUR_DOMAIN_HERE.api.affinitylive.com";

38
index.html.example Normal file
View File

@ -0,0 +1,38 @@
<form method="POST" action="affinitylive.php">
<fieldset class="checkbox">
<legend>I need:</legend>
<label>
<input type="checkbox" name="selection[]" value="quote">
A Quote
</label>
<label>
<input type="checkbox" name="selection[]" value ="help">
Help
</label>
</fieldset>
<fieldset class ="user-input">
<legend>Contact Info:</legend>
<input type="text" name="company_name" class="input" placeholder="Company Name">
<input type="text" name="contact_firstname" class="input" placeholder="First Name">
<input type="text" name="contact_surname" class="input" placeholder="Last Name">
<input type="email" name="email" class="input" placeholder="Email">
<input type="tel" name="phone_number" class="input" placeholder="Phone Number">
<div><button name="submit">Submit</button></div>
</fieldset>
</form>