APIs & Integrations

trainoasis
Participant

Update an existing contact created via custom PHP

Hello.

We have a custom non-hubspot form and when submitted behind the scenes we also add a contact in Hubspot with this custom function which is taken mostly from https://developers.hubspot.com/docs/methods/forms/submit_form:

 

 

function add_customer_to_hubspot($POST_ARRAY)
{
	$HS_portal_ID = 'our-portal-ID';
	$HS_form_guid = 'our-form-GUID';

	//Process a new form submission in HubSpot in order to create a new Contact.
	$hubspotutk      = $_COOKIE['hubspotutk']; //grab the cookie from the visitors browser.
	$ip_addr         = $_SERVER['REMOTE_ADDR']; //IP address too.
	$hs_context      = array(
	    'hutk' => $hubspotutk,
	    'ipAddress' => $ip_addr,
	    'pageUrl' => 'our-page-url',
	    'pageName' => 'My customers'
	);
	$hs_context_json = json_encode($hs_context);

	//Need to populate these variable with values from the form.
	$str_post = http_build_query($POST_ARRAY) 
	    . "&hs_context=" . urlencode($hs_context_json); // Leave this one be

	//replace the values in this URL with your portal ID and your form GUID
	$endpoint = 'https://forms.hubspot.com/uploads/form/v2/'.$HS_portal_ID.'/'.$HS_form_guid;

	$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);
	return $status_code;
}

Now, we want to allow our logged in user's to update their customers' data for each submitted customer. The actual editing happens in our CMS where the data also resides, but we also need the data in HS and it must be synced with edits as well.

What would be the best approach with the current situation?

Also, is there a better solution for both, so I don't have to have a custom update in my code + additional update for HS.

 

thanks for any info 🙂

 

0 Upvotes
4 Replies 4
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Update an existing contact created via custom PHP

Hey @trainoasis,

 

Correct me if I'm wrong, but it sounds like you submit to the HubSpot forms API immediate after recieving a submission from your website, and you'd also like to push any additional updates made by your users to that contact into HubSpot?

 

Since these updates made by your users don't correspond to a form submission, I wouldn't recommend making additional form submissions for each new update made in your CMS. Instead, I'd recommend updating the corresponding contact records by using the Contacts API. I'm not familiar with your CMS, but you could trigger this update whenever the data is "saved." This would keep your HubSpot contact records in sync with the changes made by your users in the CMS.

0 Upvotes
trainoasis
Participant

Update an existing contact created via custom PHP

Hey @Derek_Gervais, thanks for your answer. Indeed you understood correctly. Contacts API sounds like the way to go yes.

 

I will need to figure oauth authorization first (need to get a general token to use somehow?) and then figure out the PHP implementation for this via curl I reckon.

There's no overall examples for both of these two, right?

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Update an existing contact created via custom PHP

Hey @trainoasis ,

 

We don't have any PHP examples specifically, but we do have an OAuth quickstart guide written in Node.js that you can probably draw inspiration from: https://developers.hubspot.com/docs/methods/oauth2/oauth2-quickstart

Additionally, there are a few community-driven libraries for the HubSpot API; I'd recommend checking out this one: https://github.com/ryanwinchester/hubspot-php

0 Upvotes
trainoasis
Participant

Update an existing contact created via custom PHP

Oh, that's too bad. Thanks anyway ofcourse, will hopefully be able to dig into that soon.

0 Upvotes