Current Path :
/
home
/
zrcdevco
/
public_html
/
medrio
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/zrcdevco/public_html/medrio/excelreadtest-ua.php
<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(~0); set_time_limit(0); $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); // Check if file already exists if (file_exists($target_file)) { echo "Sorry, file already exists."; $uploadOk = 0; } // Check file size if ($_FILES["fileToUpload"]["size"] > 500000) { echo "Sorry, your file is too large."; $uploadOk = 0; } // Allow Excel file formats if($imageFileType != "xls" && $imageFileType != "xlsx" ) { echo "Sorry, only Excel files are allowed."; $uploadOk = 0; } // Check if $uploadOk is set to 0 by an error if ($uploadOk == 0) { echo "Sorry, your file was not uploaded."; // if everything is ok, try to upload file } else { if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { echo "<p>The file <b>". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). "</b> has been uploaded.</p>"; } else { echo "<p>Sorry, there was an error uploading your file.</p>"; } } // Test program to read in a test excel sheet and then upload to the test HubSpot Account /* data to upload to HS: * - first name (6), last name (7), email (10), phone number (11), company name (3 - Reader Company), street address , content_title(unique), content_topic (unique) */ require 'vendor/autoload.php'; // exclude companies - includes Medrio and Competitors $excludeCompanies = array("Medrio","Trionia"); $contactList = array(); use PhpOffice\PhpSpreadsheet\Spreadsheet; $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); $spreadsheet = $reader->load($target_file); $sheetData = $spreadsheet->getActiveSheet()->toArray(); $i=1; //unset($sheetData[0]); // removes the header row (first row) foreach ($sheetData as $t) { // process element here; // access column by index // create a new array removing duplicates (using email address) and excluded companies $key = $t[7]; // Email is the unique key for all users if (!in_array($t[2],$excludeCompanies)) { if (isset($contactList[$key])) { // duplicate contact // summarize content_tags - remove duplicates $contactList[$key]["content_title"] .= ",".$t[11]; $tempContactTitle = explode(",",$contactList[$key]["content_title"]); $contactList[$key]["content_title"] = implode(",",array_unique($tempContactTitle)); // summarize content_tags - remove duplicates $contactList[$key]["content_topic"] .= ",".$t[15]; // echo $contactList[$key]["content_topic"]." <br>"; $tempContactTopic = explode(",",$contactList[$key]["content_topic"]); $contactList[$key]["content_topic"] = implode(",",array_unique($tempContactTopic)); $contactList[$key]["number_of_engagements"]++; //Increment Engagement // echo $i."-Old Contact-".$t[2].",".$t[4].",".$t[5].",".$t[7]." <br>"; } else { // new contact // echo $i."-New Contact-".$t[2].",".$t[4].",".$t[5].",".$t[7]." <br>"; $contactEmails[]=$key; // Clean up Country field and only keep US and CA States $country = ucwords(strtolower($t[10])); if (($country == "United States")||($country == "Canada")) { $state = $t[9]; } else { $state=""; } $contactList[$key] = array("firstname"=>$t[4],"lastname"=>$t[5],"company"=>$t[2],"jobtitle"=>$t[6],"email"=>$t[7],"phone"=>"","content_title"=>$t[11],"content_topic"=>$t[15],"city"=>$t[8],"state"=>$state,"country"=>$country,"number_of_engagements"=>1); } } $i++; } // Update HubSpot Contacts use HubSpot\Factory; use HubSpot\Client\Crm\Contacts\ApiException; use HubSpot\Client\Crm\Contacts\Model\Filter; use HubSpot\Client\Crm\Contacts\Model\FilterGroup; use HubSpot\Client\Crm\Contacts\Model\PublicObjectSearchRequest; use HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput; // Setup Filters to check if contact in database? $filter1 = new Filter([ 'value' => '', 'property_name' => 'email', 'operator' => 'EQ' ]); $filterGroup1 = new FilterGroup([ 'filters' => [$filter1] ]); $publicObjectSearchRequest = new PublicObjectSearchRequest([ 'filter_groups' => [$filterGroup1], 'properties' => ['content_title','content_topic','number_of_engagements'] ]); // test client $client = Factory::createWithAccessToken("pat-na1-e33479fd-da09-4d3f-b7ba-883334bb0751"); // production client //$client = Factory::createWithAccessToken("CLIENT-ACCESS-TOKEN-HERE"); $contactInput = new \HubSpot\Client\Crm\Contacts\Model\SimplePublicObjectInput(); foreach ($contactList as $c) { $contactInput->setProperties([ "company" => $c["company"], "firstname" => $c["firstname"], "lastname" => $c["lastname"], "jobtitle" => $c["jobtitle"], "phone" => $c["phone"], "email" => $c["email"], "city" => $c["city"], "state" => $c["state"], // clear if country no US OR CA "country" => $c["country"], // set to proper case "content_title" => $c["content_title"], // check for dups on update "content_topic" => $c["content_topic"], // check for dups on update "number_of_engagements" => $c["number_of_engagements"], ]); // Check for client in HubSpot - searching by email address // If user is new Create the user otherwise Update the user $filter1['value']= $c["email"]; try { $apiResponse = $client->crm()->contacts()->searchApi()->doSearch($publicObjectSearchRequest); // Contact not in HubSpot database if (empty($apiResponse["results"])) { // Create a new user $contact = $client->crm()->contacts()->basicApi()->create($contactInput); echo "Added -".$c["email"]." to HubSpot <br>"; } else { // Contact is in HubSpot - update using Contact ID $contactId = $apiResponse["results"][0]["id"]; // Update existing user $c["content_title"] .= $apiResponse["results"][0]["properties"]["content_title"]; $tempContactTitle = explode(",",$c["content_title"]); $c["content_title"] = implode(",",array_unique($tempContactTitle)); // echo "Content Topic is ".$apiResponse["results"][0]["properties"]["content_topic"]."\n"; $c["content_topic"] .= $apiResponse["results"][0]["properties"]["content_topic"]; $tempContactTopic = explode(",",$c["content_topic"]); $c["content_topic"] = implode(",",array_unique($tempContactTopic)); // echo "Number of Engagements is ".$apiResponse["results"][0]["properties"]["number_of_engagements"]."\n"; $c["number_of_engagements"] += $apiResponse["results"][0]["properties"]["number_of_engagements"]; $contactInput->setProperties([ "content_title" => $c["content_title"], // check for dups on update "content_topic" => $c["content_topic"], // check for dups on update "number_of_engagements" => $c["number_of_engagements"], ]); // var_dump($contactInput); // now with Client ID we can update echo "Updated -".$c["email"]." to HubSpot <br>"; try { $apiResponse = $client->crm()->contacts()->basicApi()->update($contactId, $contactInput); // var_dump($apiResponse); } catch (ApiException $e) { echo "Exception when calling basic_api->update: ", $e->getMessage(); } } } catch (ApiException $e) { echo "Exception when calling search_api->do_search: ", $e->getMessage(); } } // Create a List and add the Contacts from this Upload to the list echo "<h3>Contact List</h3>"; // get date for HubSpot list name date $processDate = date("Y-m-d",strtotime('today')); $listDate = str_replace('-','',$processDate); // create & initialize a curl session to retriev a new list $ch = curl_init(); $authorization = "Authorization: Bearer pat-na1-e33479fd-da09-4d3f-b7ba-883334bb0751"; curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization)); // set our url with curl_setopt() curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $post=json_encode(array('name' => $listDate.'-upload-staticlist', 'dynamic'=>'false')); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = json_decode(curl_exec($ch)); curl_close($ch); //echo(json_decode($result)); $listId=$result->listId; echo "List ID is = ".$listId."</br>"; // Add all users to list with email address $ch = curl_init(); $authorization = "Authorization: Bearer pat-na1-e33479fd-da09-4d3f-b7ba-883334bb0751"; curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization)); // set our url with curl_setopt() curl_setopt($ch, CURLOPT_URL, "https://api.hubapi.com/contacts/v1/lists/".$listId."/add"); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $post=json_encode(array('emails' => $contactEmails)); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $result = json_decode(curl_exec($ch)); curl_close($ch); var_dump($result);