APIs & Integrations

hmdev
Member

PHP CURL API call suddenly stopped working with 400 error

We have an API call to add an item to a contact's property that was working great up until recently. I'm wondering if there's an API update that cause our code to break. We're suddenly receiving a 400 error and know that the email address for the contact is correct.

Here are the functions we are running

private function call_curl($post_json, $endpoint, $return_type = 'string', $custom_req = 'POST') {
    $ch = @curl_init();

    // Default is "POST", but allow for options like "DELETE"
    // -------------------------------------------------------------------------------
    if($custom_req != 'POST') {
        @curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $custom_req);
    } else {
        @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);
    $response = @curl_exec($ch);
    $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_errors = curl_error($ch);
    @curl_close($ch);

    if($return_type == 'response') {
        return $response;
    } else {
        return $this->process_status_code($status_code, $return_type);
    }
}

public function create_or_update_contact($email, $properties, $return_type = 'string') {
    if($email && $properties) {
        $post_json = json_encode($properties);
        $endpoint = 'https://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/'.$email.'?hapikey='.$this->hapikey;

        return $this->call_curl($post_json, $endpoint, $return_type);
    }

    return $this->default_error;
}

Here is the data that we're passing in

$args = array(
'properties' => array(
array(
'property' => 'resources_download',
'value' => 'Resource Title'
)
)
);

0 Upvotes
1 Reply 1
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

PHP CURL API call suddenly stopped working with 400 error

Hi @hmdev,

Are you getting this error consistently? Can you give me the full error you're seeing, and the specific endpoint you're hitting when you get the error?

0 Upvotes