How to use createOrUpdate contact method in hubspot in version 4 in PHP ,2024

The create or update a contact endpoint is used to create a new HubSpot contact or update an existing one. in Hubspot v3 createOrUpdate method by email id has been removed.

So in this article, we will let you know to use createOrUpdate() contact method in v3 in PHP.let’s start —

Step 1: Curl post method with header

#---------------- Curl with  Headder Post ==========================================

function headers_httppost($url, $arr_data, $headers)
{

$data = json_encode($arr_data);

#echo "<pre>";print_r($data);die;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($arr_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);

#print_r($result);

return json_decode($result);
}

Step 2: Curl patch method with header

#=====================  patch method for http curl =========================================================================================
function headers_http_patch($url, $arr_data, $headers)
{

$data = json_encode($arr_data);

#echo "<pre>";print_r($data);die;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($arr_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);

#print_r($result);

return json_decode($result);
}

Step 3: Get contact data with contact id via email id New HubSpot private key

#-------------- get data with contact id via email id New Hubspot private key =====================================

function get_contactid_via_email_newhubspot_contact($email_id, $private_key)
{

$headers          = [
	'Content-Type: application/json',
	'Authorization: Bearer ' . $private_key,
];

$url   =  'https://api.hubapi.com/crm/v3/objects/contacts/search?';

$filter1['filters'][] = ['propertyName' => 'email', 'operator' => 'EQ', 'value' => $email_id];

$data_1['filterGroups']     =  [$filter1];
$data_1['properties']       =  ['id', 'email', 'firstname'];
#$data_1['query']           =  'string';
$data_1['limit']            =  1;
$data_1['after']            =  0;

$all_data  =   $this->headers_httppost($url, $data_1, $headers);

return $all_data;
}

Step 4: method to create or update HubSpot contact data via using email id

	function hubspot_create_update_contact_hook($email_id, $prop_data, $private_key)
{
#============ check data exist or not =========================================================
$get_details              = $this->get_contactid_via_email_newhubspot_contact($email_id, $private_key);
$headers                  = [
'Content-Type: application/json',
'Authorization: Bearer ' . $private_key,
];
#=================== if is empty then create contact ====================================================================
if (empty($get_details->total)) {
$url                       = 'https://api.hubapi.com/crm/v3/objects/contacts?';
$data['properties']        = $prop_data;
$res_data                  = $this->headers_httppost($url, $data, $headers);
} else {
#====================== Update contact details via contact id =======================================
$contact_id                = $get_details->results[0]->id;
$url                       = "https://api.hubapi.com/crm/v3/objects/contacts/$contact_id?";
$data['properties']        = $prop_data;
#print_r($data);die;
$res_data                  = $this->headers_http_patch($url, $data, $headers);
}
return $res_data;


}

Final Code with class in PHP

class Piyush{


#---------------- Curl with  Headder Post ==========================================

function headers_httppost($url, $arr_data, $headers)
{

$data = json_encode($arr_data);

#echo "<pre>";print_r($data);die;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($arr_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);

#print_r($result);

return json_decode($result);
}




#=====================  patch method for http curl =========================================================================================
function headers_http_patch($url, $arr_data, $headers)
{

$data = json_encode($arr_data);

#echo "<pre>";print_r($data);die;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($arr_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);

#print_r($result);

return json_decode($result);
}





#-------------- get data with contact id via email id New Hubspot private key =====================================

function get_contactid_via_email_newhubspot_contact($email_id, $private_key)
{

$headers          = [
	'Content-Type: application/json',
	'Authorization: Bearer ' . $private_key,
];

$url   =  'https://api.hubapi.com/crm/v3/objects/contacts/search?';

$filter1['filters'][] = ['propertyName' => 'email', 'operator' => 'EQ', 'value' => $email_id];

$data_1['filterGroups']     =  [$filter1];
$data_1['properties']       =  ['id', 'email', 'firstname'];
#$data_1['query']           =  'string';
$data_1['limit']            =  1;
$data_1['after']            =  0;

$all_data  =   $this->headers_httppost($url, $data_1, $headers);

return $all_data;
}



	function hubspot_create_update_contact_hook($email_id, $prop_data, $private_key)
								{
									#============ check data exist or not =========================================================
									$get_details              = $this->get_contactid_via_email_newhubspot_contact($email_id, $private_key);
									$headers                  = [
										'Content-Type: application/json',
										'Authorization: Bearer ' . $private_key,
									];
									#=================== if is empty then create contact ====================================================================
									if (empty($get_details->total)) {
										$url                       = 'https://api.hubapi.com/crm/v3/objects/contacts?';
										$data['properties']        = $prop_data;
										$res_data                  = $this->headers_httppost($url, $data, $headers);
									} else {
										#====================== Update contact details via contact id =======================================
										$contact_id                = $get_details->results[0]->id;
										$url                       = "https://api.hubapi.com/crm/v3/objects/contacts/$contact_id?";
										$data['properties']        = $prop_data;
										#print_r($data);die;
										$res_data                  = $this->headers_http_patch($url, $data, $headers);
									}
									return $res_data;
								

								}


}

Discover more from STRUGGLER KING.COM

Subscribe now to keep reading and get access to the full archive.

Continue Reading