From 65d05ece68d1a646e246a10348dc5e4db4d035f5 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Wed, 3 Sep 2014 09:37:26 -0700 Subject: [PATCH] Initial commit --- .gitignore | 2 + README.md | 15 ++++ affinitylive.php | 215 +++++++++++++++++++++++++++++++++++++++++++++ config.php.example | 7 ++ index.html.example | 38 ++++++++ 5 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 affinitylive.php create mode 100644 config.php.example create mode 100644 index.html.example diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b006de --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +index.html +config.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b4d81f --- /dev/null +++ b/README.md @@ -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. diff --git a/affinitylive.php b/affinitylive.php new file mode 100644 index 0000000..26ae866 --- /dev/null +++ b/affinitylive.php @@ -0,0 +1,215 @@ + + + + 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); + } + + +?> +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; +} \ No newline at end of file diff --git a/config.php.example b/config.php.example new file mode 100644 index 0000000..e618843 --- /dev/null +++ b/config.php.example @@ -0,0 +1,7 @@ + + +
+ + I need: + + + + + +
+ +
+ + Contact Info: + + + + + + + + + + + +
+ +
+