APIs & Integrations

golinka
Member

Problem with hapikey

@pmanca please tell me what I have not added to the contacts when I change hapikey? I have 2 projects. I realized at one adding contacts, and the second insert the same code, changing only hapikey, but the contacts are not added to the two contacts list

js code:

function login() {
    var email = $('#agilefield-2').val();
    var message = '';
    if ($('input[name=remove]').prop('checked')) {
        message = nameApp + ' subscription: not requested';
    }
    $.ajax({
        type: "POST",
        url: "/" + courseDir + "/send-contact.php",
        data: {
            'email': email,
            'message': message
        },
        success: function(data) {
            console.log('succes)))');
        },
        error: function(xhr, str) {
            console.log('error)))');
        }
    })
}

php code:

$message = $_POST["message"];
$email = $_POST["email"];

if($message != null) {
    $arr = array(
        "properties" => array(
            array(
                "property" => "email",
                "value" => $email
            ),
            array(
                "property" => "lifecyclestage",
                "value" => 'opportunity'
            ),
            array(
                "property" => "message",
                "value" => $message
            )
        )
    );
} else {
    $arr = array(
        "properties" => array(
            array(
                "property" => "email",
                "value" => $email
            ),
            array(
                "property" => "lifecyclestage",
                "value" => "opportunity"
            )
        )
    );
}

$post_json = json_encode($arr);

$endpoint = "https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/".$email."/?hapikey=2489173";
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_POST, true);
@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);
@curl_exec($ch);
@curl_close($ch);
0 Upvotes
6 Replies 6
3PETE
HubSpot Employee
HubSpot Employee

Problem with hapikey

@golinka

$arr = array(
        "properties" => array(
            array(
                "property" => "email",
                "value" => $email
            ),
            array(
                "property" =>; "lifecyclestage",
                "value" =>; "opportunity"
            )
        )
    );

I don’t believe you need to create an array of arrays here.

There is some example php here on this end point. This might help you.

Submit data to a form | HubSpot Forms API

POST https://forms.hubspot.com/uploads/form/v2/:portal_id/:form_guid - Send form submission data to HubSpot. Form submissions from external sources can be made to any registered HubSpot form. You can see a list of forms on your portal by going to...

I would look at how they make their array here.

$hs_context      = array(
    'hutk' => $hubspotutk,
    'ipAddress' => $ip_addr,
    'pageUrl' => 'http://www.example.com/form-page',
    'pageName' => 'Example Title'
);

Make sure to have it formatted to match the JSON in the dev documentation.

0 Upvotes
golinka
Member

Problem with hapikey

@pmanca I do not think that’s the problem. I’ve used this code on the first project and everything works fine. When I moved the code into a new project, my form does not add a contact in the list.

But I will try your option

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Problem with hapikey

Are you trying to add contacts which already exist? Did you move over your php file as well?

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Problem with hapikey

@golinka do both projects go to the same portal?

0 Upvotes
golinka
Member

Problem with hapikey

@pmanca No, on different. on different domains, if you have it. but added in a single user crm

0 Upvotes
3PETE
HubSpot Employee
HubSpot Employee

Problem with hapikey

@golinka Does the second portal have a message as a custom property as well? Do you get a response code back when you try it on the non-working portal?

0 Upvotes