APIs & Integrations

pcrivera21
Member

Please Help me to solve myproblem

I am trying to build it in Php. So far my code based only on what I learn through self-studying.
Please correct me if my code will work and please give me for the code that I am stacked.

Steps:
When someone registers the webhook will trigger:

Inside the webhook, I put Oath code:
//The username and password must match what you enter in your HubSpot workflow webhook
$webhookUsername = “my_username”;
$webhookPassword = “**************”;

$hubspotApiKey = “myhapikey”;

if ($_SERVER[‘PHP_AUTH_USER’] != $webhookUsername || $_SERVER[‘PHP_AUTH_PW’] != $webhookPassword || $_SERVER[‘REQUEST_METHOD’] != ‘POST’){
exit;
}
Getting the details of registered:
$json = $HTTP_RAW_POST_DATA;

$array = json_decode($json, true);

$contactId = $array[‘vid’];
//set the company id if it exists
if($array[‘properties’][‘email’][‘value’] != null){
$emailId = $array[‘properties’][‘email’][‘value’];
}
else{
$emailId = null;
}
Getting the contact email which I will use for update

$url_owners = “http://api.hubapi.com/owners/v2/owners?hapikey=$hubspotApiKey”;
Stacked: Because it gives only my owner details and not all the owners.

  1. Getting all owners and update all the owner’s details in our database.
  2. Running the function where our “own rules” of assigning leads of each owner.

$taskPostDataArray = array(
“properties” => array(
“property”=>“hubspot_owner_id”,
“value”=>$ownerId_from_db
)
);

$taskPostDataJson = json_encode($taskPostDataArray);

$url = “https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/$emailId/?hapikey=$hubspotApiKey”;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $taskPostDataJson);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
Stacked: trying to update the contact properties hubspot_owner_id to assign the lead to the proper agent.

Please correct my codes and answer some stacked process.

0 Upvotes
3 Replies 3
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Please Help me to solve myproblem

Hi @Paul_Rivera,

I’m not proficient in PHP, so I’m not the best resource for troubleshooting your code directly. I’d be happy to help with any HubSpot API issues/questions, but for troubleshooting code you’d be better served elsewhere. I can say that $HTTP_RAW_POST_DATA has been deprecated as of PHP 5.6.0, so you should try php://input instead:
http://php.net/manual/en/reserved.variables.httprawpostdata.php

0 Upvotes
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Please Help me to solve myproblem

Hi @Paul_Rivera,

Are you using the hapikey from your Marketing/CRM portal? It sounds like you may have been using the hapikey from your developer portal. Using the correct API key should allow you pull the owners and update contacts successfully.

0 Upvotes
pcrivera21
Member

Please Help me to solve myproblem

I found it!
btw, I want to get the current registered data before the webhook triggered.
Is this the correct code for it?
$json = $HTTP_RAW_POST_DATA;

I want to know also where can I see the fetch data while doing some testing or troubleshooting.

0 Upvotes