APIs & Integrations

Andrew_nicholas
Member

Error when Associate the contact to deal

Hey guys,

 

This is my code php and i have an error and I don't know what's wrong.Can you help me please ?!

 

Details: 

 

 

 

<?php

$hapikey = "api key is ok";
$array_put= array("fromObjectId"=> 18801, "toObjectId"=>1565549700 , "category"=> "HUBSPOT_DEFINED", "definitionId"=>4);

$put_json = json_encode($array_put);



$endpoint_deal="https://api.hubapi.com/crm-associations/v1/associations?hapikey=".$hapikey;
$cher = @curl_init();
@curl_setopt($cher, CURLOPT_PUT, true);
@curl_setopt($cher, CURLOPT_URL, $endpoint_deal);
@curl_setopt($cher, CURLOPT_PUTFIELDS, $put_json);
@curl_setopt($cher, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
@curl_setopt($cher, CURLOPT_RETURNTRANSFER, true);
$responser = @curl_exec($cher);
@curl_close($cher);
echo $put_json;
var_dump($responser);







[{"fromObjectId":18801,"toObjectId":1565549700,"category":"HUBSPOT_DEFINED","definitionId":4}]


 "{"status":"error","message":"Invalid input JSON: No content (empty input stream)",

 

0 Upvotes
9 Replies 9
LLiu2
Member

Error when Associate the contact to deal

I found the answer related this post from another post:

HubSpot Community - Status code 405 on Updating Deal, yet not on Creating - HubSpot Community

 

Following list the full source codes from my test (be aware that I still have issue with v3 end point. But v1 is fine).

<?php

function http_put($endpoint, $post_json) {
echo "END POINT: $endpoint <br>";
echo "JSON DATA: <br>$post_json <br>";

$ch = @curl_init();
@curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
@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 "RESPONSE: '$response'<br>";
echo "STATUS CODE: '$status_code'<br>";
echo "CURL ERRORS: '$curl_errors'<br>";

return $response;
}

$hapikey = "an api key here";

$dealId = "an existing deal id here";

$endpoint = "https://api.hubapi.com/deals/v1/deal/$dealId?hapikey=$hapikey"; // legacy endpoint

//$endpoint = "https://api.hubapi.com/crm/v3/objects/deals/$dealId?hapikey=$hapikey";

$json_post = '{
"properties": [
{
"name": "amount",
"value": "17961.70"
}
]
}';


$result = http_put($endpoint, $json_post);

$res = json_decode($result, true);

if($res["status"]=="error") {;
echo "<br>ERROR: ".$res["message"]."<br>";
echo "FULL RETURN MESSAGE: <br>";
print_r($res);
} else {
echo "RETURN MESSAGE: <br>";
print_r($res);
}

?>

0 Upvotes
LLiu2
Member

Error when Associate the contact to deal

Following is the related function and its output:

function http_put($endpoint, $post_json) {
echo "END POINT: $endpoint <br>";
echo "PUT JSON: <br>$post_json <br>";
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_PUT, 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 "RESPONSE: <br>'$response'<br>";
return $response;
}

 

 

END POINT: https://api.hubapi.com/crm/v3/objects/deals/7789128043?hapikey=dc8a20c1-58e1-45e0-ad94-049183e5bd66
PUT JSON:
{"properties":[{"name":"dealstage","value":"decisionmakerboughtin"},{"name":"amount","value":"17961.69921875"},{"name":"description","value":"INSULATED"}]}
RESPONSE:
''

0 Upvotes
LLiu2
Member

Error when Associate the contact to deal

I revisited this today and noticed that the response is no empty instead of 

{"status":"error","message":"Invalid input JSON: No content (empty input stream)" ...

0 Upvotes
LLiu2
Member

Error when Associate the contact to deal

Sorry there is a typo: 

... the response is empty instead of ... 

 

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

Error when Associate the contact to deal

Hey @Andrew_nicholas,

 

Based on your code it looks like you're sending an array json PUT request to the Associate CRM objects endpoint

 

The endpoint only accept JSON e.g. 

{"fromObjectId":118401,
"toObjectId":1563543119,
"category":"HUBSPOT_DEFINED",
"definitionId":4}

Could you try sending a JSON body and see if it works? 

0 Upvotes
Andrew_nicholas
Member

Error when Associate the contact to deal

Hey  ,

 

Give me the same error. But I convert the array in json with this function json_encode() in my code.

Another solution you know?

 

Thanks for the reply.

0 Upvotes
WendyGoh
HubSpot Employee
HubSpot Employee

Error when Associate the contact to deal

Hey @Andrew_nicholas,

 

That's strange. 

 

Could we try using single quote instead of double quote? E.g.

 

$array_put= array('fromObjectId'=> 18801, 'toObjectId'=>1565549700 , 'category'=> 'HUBSPOT_DEFINED', 'definitionId'=>4);

$put_json = json_encode($array_put);
0 Upvotes
LLiu2
Member

Error when Associate the contact to deal

I have been having the same exact issue. I tried the simplest example of updating amount of a deal.  

JSON body is as below:

{ "properties": [ { "name": "amount", "value": "70000" } ] }

RESPONSE returned:

{"status":"error","message":"Invalid input JSON: No content (empty input stream)","correlationId":"1ba7fd6b-8bf2-4b98-xxxxxxxxxxxxxxxxx"}

 

I am using the same php curl put method as what Andrew_nicholas used.

 

Thanks.

 

0 Upvotes
Andrew_nicholas
Member

Error when Associate the contact to deal

 

Hey WendyGoY,

 

I'm trying but for nothing.

0 Upvotes