APIs & Integrations

Not applicable

Create new contact by hubspot api on server but response not success

Create new contact by hubspot api on server but response not success .....
On localhost server working properly and get contacts in Hubspot contacts but not working on live cpanel hosting why ? please guide me thank you..

My code is .... this code is working on localhost perfectly but not on live server

		$arr = array(
		            'properties' => array(
		                array(
		                    'property' => 'email',
		                    'value' => 'klida@gmail.com'
		                ),
		                array(
		                    'property' => 'firstname',
		                    'value' => 'user'
		                ),
		                array(
		                    'property' => 'lastname',
		                    'value' =>'ss'
		                ),
		                array(
		                    'property' => 'address',
		                    'value' => 'khalida	'
		                ),
		                array(
		                    'property' => 'zip',
		                    'value' => '2300'
		                ),
		                array(
		                    'property' => 'phone',
		                    'value' => '009125500'
		                )
		            )
		        );
		        $post_json 	= json_encode($arr,JSON_PRETTY_PRINT);
		     
		        $hapikey 	= 'REDACTED';
		        //$endpoint 	= 'https://api.hubapi.com/contacts/v1/contact?hapikey='.$hapikey;
        		$endpoint = 'https:api.hubapi.com/forms/v2/forms?hapikey='.$hapikey;
        
		        $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);
		        $response 	 = @curl_exec($ch);
		        $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
		        $curl_errors = curl_error($ch);
		        @curl_close($ch);
		        echo "<pre>";
		        echo "curl Errors: " . $curl_errors;
		        echo "\nStatus code: " . $status_code;
		        echo "\nResponse: " . $response;
		        echo "</pre>";

		        return $response;
		        
}
0 Upvotes
1 Reply 1
Derek_Gervais
HubSpot Alumni
HubSpot Alumni

Create new contact by hubspot api on server but response not success

Hi @khalidkhanjee,

Are you seeing any specific errors? Can you share the error code / error body you're seeing when these requests fail?

I'm not particularly familiar with PHP, but what exactly does the final JSON body look like? It seems like you might be including the properties array inside another array, instead of an object? The final JSON body should appear like this:

{
  "properties": [
    {
      "property": "email",
      "value": "testingapis@hubspot.com"
    },
    {
      "property": "firstname",
      "value": "Adrian"
    },
    {
      "property": "lastname",
      "value": "Mott"
    },
    {
      "property": "website",
      "value": "http://hubspot.com"
    },
    {
      "property": "company",
      "value": "HubSpot"
    },
    {
      "property": "phone",
      "value": "555-122-2323"
    },
    {
      "property": "address",
      "value": "25 First Street"
    },
    {
      "property": "city",
      "value": "Cambridge"
    },
    {
      "property": "state",
      "value": "MA"
    },
    {
      "property": "zip",
      "value": "02139"
    }
  ]
}

Also, please refrain from including your hapikey in forum posts. Since this is a public forum, including authentication details can compromise your portal's security.

0 Upvotes