# PHP New Engagement Bad Request

**URL:** <https://community.hubspot.com/t/php-new-engagement-bad-request/17492>\
**Category:** APIs & Integrations\
**Tags:** contacts\
**Created:** [November 30, 2019, 11:49am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492 "2019-11-30T11:49:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [November 30, 2019, 11:49am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/1 "2019-11-30T11:49:10Z")

</div>

Hey all,

I am new to HubSpot and build a little php class where I can add contacts and now I want to add notes to contacts but I get a “bad request”.

Here is my json code:

```auto
{
	"engagement": {
		"active": true,
		"type": "NOTE",
		"timestamp": 1575112787442
	},
	"associations": {
		"contactIds": [
			1351
		],
		"companyIds": [],
		"dealIds": [],
		"ownerIds": []
	},
	"metadata": {
		"body": "Meine Notiz :-)!"
	}
}

```

Which is build with this php code:

```auto
try {
	$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
	$lTimeStampNow = round(microtime(true) * 1000);

	$lEngagement['engagement'] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
	$lEngagement['associations'] = array( "contactIds" => array($pHubSpotContactId), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
	$lEngagement['metadata'] = array("body" => "$pNoteText");
	$lEngagementAsJSON = json_encode($lEngagement);

	$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
		CURLOPT_HTTPHEADER => $header,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_POST => true,
		CURLOPT_POSTFIELDS => $lEngagementAsJSON
	));
	$response = curl_exec($curl);
	curl_close($curl);
	print_r($response);
	print_r($lEngagementAsJSON);
}
catch(Exception $e) {
	return $e->getMessage();
}

```

I hope someone can find the mistake, I cant see it 😕

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [November 30, 2019, 8:31pm UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/2 "2019-11-30T20:31:32Z")

</div>

Hello @euunit  
Can you try replacing single quotes to double for the array indexes of engagement, association and metadata. Hope it can resolve the problem.  
Thanks

---

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [November 30, 2019, 10:21pm UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/3 "2019-11-30T22:21:00Z")

</div>

Hello @himanshurauthan

That didn’t helped me 😕

My php Code now looks like that

```auto
try {
	$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
	$lTimeStampNow = round(microtime(true) * 1000);

	$lEngagement["engagement"] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
	$lEngagement["associations"] = array( "contactIds" => array($pHubSpotContactId), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
	$lEngagement["metadata"] = array("body" => "$pNoteText");
	$lEngagementAsJSON = json_encode($lEngagement);

	$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
		CURLOPT_HTTPHEADER => $header,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_POST => true,
		CURLOPT_POSTFIELDS => $lEngagementAsJSON
	));
	$response = curl_exec($curl);
	curl_close($curl);
	print_r($response);
	print_r($lEngagementAsJSON);
}
catch(Exception $e) {
	return $e->getMessage();
}

```

And the JSON lokks still like that

```auto
{
	"engagement": {
		"active": true,
		"type": "NOTE",
		"timestamp": 1575152265002
	},
	"associations": {
		"contactIds": [
			1351
		],
		"companyIds": [],
		"dealIds": [],
		"ownerIds": []
	},
	"metadata": {
		"body": "Meine Notiz :-)!"
	}
}

```

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [December 2, 2019, 4:24am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/4 "2019-12-02T04:24:41Z")

</div>

Hello @euunit

Can you please once check for this line in the code?

`$lEngagement['metadata'] = array("body" => "$pNoteText");`

I can not see the $pNoteText defined anywhere in the code. As its the required parameter in the API request so may be an empty value can be causing the Bad Request Error.

Thanks

---

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [December 2, 2019, 6:35am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/5 "2019-12-02T06:35:42Z")

</div>

Hey @himanshurauthan

I changed the parameters to hard coded text like that but nothing changed.

BUT! When i Take the JSON and put it with the access token for oauth 2 in Postman-programm, the note will be created :-O!!

So the Json is correct and my post-curl is wrong?

Json

`{"engagement":{"active":true,"type":"NOTE","timestamp":1575268931219},"associations":{"contactIds":[1351],"companyIds":[],"dealIds":[],"ownerIds":[]},"metadata":{"body":"My Text"}}`

```auto
try {
	$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
	$lTimeStampNow = round(microtime(true) * 1000);

	$lEngagement["engagement"] = array("active" => true,"type" => "NOTE","timestamp" => $lTimeStampNow);
	$lEngagement["associations"] = array( "contactIds" => array(1351), "companyIds" => array(), "dealIds" => array(), "ownerIds" => array());
	$lEngagement["metadata"] = array("body" => "My Text");
	$lEngagementAsJSON = json_encode($lEngagement);

	$header = array("Content-Type: application/json;Authorization: Bearer {$this->AccessToken}");
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
		CURLOPT_HTTPHEADER => $header,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_POST => true,
		CURLOPT_POSTFIELDS => $lEngagementAsJSON
	));
	$response = curl_exec($curl);
	curl_close($curl);
	print_r($response);
	print_r($lEngagementAsJSON);
}
catch(Exception $e) {
	return $e->getMessage();
}

```

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [December 2, 2019, 6:44am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/6 "2019-12-02T06:44:29Z")

</div>

```auto
$note_details = array(
			"engagement" => array(
				"active" => true,
				"type" => "NOTE",
				"timestamp"	=> 5456563646
			),
			"associations" => array(
				"contactIds" => array( 1234 ),
			),
			"metadata" => array(
				"body" => "TESTING",
			)
		);

```

@euunit Try to use the above format once and see if anything works.

---

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [December 2, 2019, 7:17am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/7 "2019-12-02T07:17:59Z")

</div>

@himanshurauthan I tryed it but that wont work. I tryed this but that still does not work. But this is the json which works in postman. So it has to do something with the header maybe?

```auto
try {
	$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);
	$lTimeStampNow = round(microtime(true) * 1000);

	$lEngagementAsJSON = '{"engagement":{"active":true,"type":"NOTE","timestamp":5456563646},"associations":{"contactIds":[1351]},"metadata":{"body":"TESTING"}}';
	$header = array("Content-Type: application/json; Authorization: Bearer {$this->AccessToken}");
	$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_POST => true,
		CURLOPT_POSTFIELDS => $lEngagementAsJSON,				
		CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
		CURLOPT_HTTPHEADER => $header,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_RETURNTRANSFER => true
	));
	$response = curl_exec($curl);
	curl_close($curl);
	print_r($response);
	print_r($lEngagementAsJSON);
}
catch(Exception $e) {
	return $e->getMessage();
}

```

---

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [December 2, 2019, 7:29am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/8 "2019-12-02T07:29:56Z")

</div>

@himanshurauthan I dont know but my two last replys were deletet? I cant see them.

So the code from you dont work and when I put the json as string to the fields option it wont work but in postman it works.

Maybe its something with my header?

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [December 3, 2019, 4:59am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/9 "2019-12-03T04:59:29Z")

</div>

@euunit

Try using this for headers:

```auto
$headers = array(
	'Content-Type: application/json',
	'Authorization: Bearer '.$access_token
);

```

Thanks

---

<div class="post-metadata">

**Author:** ![euunit](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/euunit/32/58551_2.png) [@euunit](https://community.hubspot.com/u/euunit)\
**Post date:** [December 3, 2019, 6:19am UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/10 "2019-12-03T06:19:23Z")

</div>

@himanshurauthan you are genius! With this header array it works with the first function I wrote. Thank you so much man :-)!!

For those with similar problems here is my full function:

```auto
public function sendNoteToHubSpot($pHubSpotContactId, $pKundenID, $pNoteText, $pPathChange = '') {
	try {
		$this->CheckAccessTokenGueltigOderNeuesHolen($pKundenID,$pPathChange);

		$lTimeStampNow = round(microtime(true) * 1000);
		$lEngagement = array(
			"engagement" => array(
				"active" => true,
				"type" => "NOTE",
				"timestamp" => $lTimeStampNow
			),
			"associations" => array(
				"contactIds" => array($pHubSpotContactId)
			),
			"metadata" => array(
				"body" => $pNoteText
			)
		);
		$lEngagementAsJSON = json_encode($lEngagement);

		$headers = array(
			'Content-Type: application/json',
			'Authorization: Bearer '.$this->AccessToken
		);

		$curl = curl_init();
		curl_setopt_array($curl, array(
			CURLOPT_URL => $this->ENDPOINT_POST_ENGAGEMENTS,
			CURLOPT_HTTPHEADER => $headers,
			CURLOPT_SSL_VERIFYPEER => false,
			CURLOPT_RETURNTRANSFER => true,
			CURLOPT_POST => true,
			CURLOPT_POSTFIELDS => $lEngagementAsJSON
		));
		$response = curl_exec($curl);
		curl_close($curl);

		return json_decode($response, true);		
	}
	catch(Exception $e) {
		return $e->getMessage();
	}
}

```

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [December 3, 2019, 1:52pm UTC](https://community.hubspot.com/t/php-new-engagement-bad-request/17492/11 "2019-12-03T13:52:36Z")

</div>

Hello @euunit

Good to hear that your code works now. All the best.

Thanks.
