$hubspotutk, 'ipAddress' => $ip_addr, 'pageUrl' => (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", 'pageName' => $pageName ); $hs_context_json = json_encode($hs_context); //Add the Hubspot data to array $PropertiesArray['hs_context'] = $hs_context_json; $str_post = http_build_query($PropertiesArray); //replace the values in this URL with your portal ID and your form GUID $endpoint = 'https://forms.hubspot.com/uploads/form/v2/'.$Hubspot_Portal_ID.'/'.$formGuid; $ch = @curl_init(); @curl_setopt($ch, CURLOPT_POST, true); @curl_setopt($ch, CURLOPT_POSTFIELDS, $str_post); @curl_setopt($ch, CURLOPT_URL, $endpoint); @curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded' )); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = @curl_exec($ch); //Log the response from HubSpot as needed. $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code @curl_close($ch); //echo $status_code . " " . $response; //echo $status_code; if (substr($status_code, 0, 1) === '2') { //checks for 200 http code return true; }else{ return false; } } //Funciton to strip non numeric character from fields (makes it easier to match conditions in Hubspot) function Non_Numeric_Strip($str) { $Stripped = preg_replace("/[^0-9.]/", "", $str); //Remove non-numeric characters - except for decimal place $Stripped = preg_replace('/[^0-9]|.[0-9]*$/','',$Stripped); //remove cents if($Stripped){ return $Stripped; }else{ return ""; } } if($HTTP_RAW_POST_DATA){ $host="localhost"; // Host name $username="Your db user"; // Mysql username $password="pass"; // Mysql password $db_name="dbname"; // Database name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Dump body $DecodedJson = json_decode($HTTP_RAW_POST_DATA, true); foreach($DecodedJson as $key =>$value){ //Get the data we want fromt he array $email = ''; if(isset($value['email'])){ $email=mysql_real_escape_string($value['email']); } $event = ''; if(isset($value['event'])){ $event=mysql_real_escape_string($value['event']); } $url = ''; if(isset($value['url'])){ $url=mysql_real_escape_string($value['url']); } $reason = ''; if(isset($value['reason'])){ $reason=mysql_real_escape_string($value['reason']); } $type = ''; if(isset($value['type'])){ $type=mysql_real_escape_string($value['type']); } //Log activity into a database if you like $sql=" INSERT INTO `user_activity` (`id`, `email`, `event`, `newsletter_id`, `url`, `categories`, `dateadded`) VALUES (NULL, '$email', '$event', '$newsletterid', '$url', '', CURRENT_TIMESTAMP); "; $result=mysql_query($sql); if($event=="click" OR $event=="open"){ //add contact to Hubspot API - //$Contact_Properties['insurance_contents_sum_required'] = Non_Numeric_Strip('25,000'); $Contact_Properties = array(); $Contact_Properties['email'] = $email; //Minimum Required echo 'click or open'; //Get all data from main your main data list and push into Hubspot (as send grid only supplies the users email address) //Insert Data into DB $sql2="SELECT * FROM `fulldb` WHERE `Email` = '$email' LIMIT 1"; $r=mysql_query($sql2); while($i = mysql_fetch_array($r)) { //$j = $i['Email']; if($i['First_Name']){ $Contact_Properties['firstname'] = $i['First_Name']; } if($i['Last_Name']){ $Contact_Properties['lastname'] = $i['Last_Name']; } if($i['Company_Name']){ $Contact_Properties['company'] = $i['Company_Name']; } if($i['Company_Phone']){ $Contact_Properties['phone'] = $i['Company_Phone']; } if($i['Company_Website']){ $Contact_Properties['website'] = $i['Company_Website']; } if($i['Job_Title']){ $Contact_Properties['jobtitle'] = $i['Job_Title']; } if($i['Job_Category']){ $Contact_Properties['job_category'] = $i['Job_Category']; } if($i['Mobile']){ $Contact_Properties['mobilephone'] = $i['Mobile']; } if($i['PostalCode']){ $Contact_Properties['zip'] = $i['PostalCode']; } if($i['State']){ $Contact_Properties['state'] = $i['State']; } if($i['Region']){ $Contact_Properties['address'] = $i['Region']; } if($i['Country']){ $Contact_Properties['country'] = $i['Country']; } break; } //Push to Hubspot $Request = HS_Form_Submit('Sendgrid','YOUR FORM ID HERE',$Contact_Properties); //See if it was a success if($Request){ echo "Success"; }else{ echo "Something went wrong"; } } //Unsubscribe them from Hubspot if they unsubscribe via Sendgrid if($event=="group_unsubscribe"){ echo 'unsub'.$email; $Hubspot_Portal_ID = '66666666'; $arr = array( 'unsubscribeFromAll' => true ); $post_json = json_encode($arr); $hapikey = "YOURKEY HERE"; $endpoint = 'https://api.hubapi.com/email/public/v1/subscriptions/'.$email.'?portalId='.$Hubspot_Portal_ID.'&hapikey=' . $hapikey; $ch = @curl_init(); //@curl_setopt($ch, CURLOPT_POST, true); @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); @curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json); @curl_setopt($ch, CURLOPT_URL, $endpoint); @curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = @curl_exec($ch); $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE); $curl_errors = curl_error($ch); @curl_close($ch); echo "curl Errors: " . $curl_errors; echo "\nStatus code: " . $status_code; echo "\nResponse: " . $response; } } // close connection mysql_close(); }else{ echo 'no'; } ?>