Hubspot form Invalid input JSON using Hubspot example JSON

Hi,

I’ve created a form in Hubspot and I’m trying to push form data to Hubspot form API using PHP CURL but I’m getting JSON error.

{“status”:“error”,“message”:“Invalid input JSON on line 1, column 1: Cannot construct instance of `com.hubspot.formsubmission.core.models.PublicApiFormSubmissionRequest$Json` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (‘{\“fields\”:[{\“name\”:\“email\”,\“value\”:\“example@example.com\”},{\“name\”:\“firstname\”,\“value\”:\“GS\”},{\“name\”:\“lastname\”,\“value\”:\“Test\”},{\“name\”:\“company\”,\“value\”:\“GS Test Co\”},{\“name\”:\“phone\”,\“value\”:\“44123456789\”}]}’)”,“correlationId”:“78010c5b-dc3f-45ba-a93e-9c3ab5da3622”}

I’ve tried posting using Hubspot’s example JSON data but that displays the same error. Also different configurations of quotes/apostrophes. I’m assuming $data is incorrectly set up but I’ve run out of options.

This is the code I’m using:

<?php
$url = 'https://api.hsforms.com/submissions/v3/integration/submit/[MyPortalID]/[TheFormGUID]';

$data = '{"fields":[{"name":"email","value":"example@example.com"},{"name":"firstname","value":"GS"},{"name":"lastname","value":"Test"},{"name":"company","value":"GS Test Co"},{"name":"phone","value":"44123456789"}]}';

$postdata = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

?>

I hope you can help.

Many thanks

@webdew , @prosa are you all seeing anything amiss?

@Reseaux

It looks like you are encoding json - (make php to json) instead of decoding json (from json to php).

Your $data string is already setup in json format and should not be converted to json from json.

I have used this function I found online to help med convert json to a php object.

function json_to_php($data) {if (is_object($data)) {$data = get_object_vars($data);}if (is_array($data)) {return array_map(__FUNCTION__, $data);}else {return $data;}}

Then I used Guzzle PHP to do my calls - but guzzle is based on CURL so mixing it up some how should make it work for you.

I might have done this more messy than it needs to be - but it works.

$json_content = json_to_php( array(json_decode($json_content))); 
$client = new \GuzzleHttp\Client(['http_errors' => false]);
$response = $client->request($method, $url, $json_content[0]);
$encrypted = $response->getBody();
$result = json_decode($encrypted, true);

But I think the issue in your case is your encoding.

Michael

Thanks Michael - much appreciated - will give that a go.

Hi Michael,

As you correctly pointed out my $data variable was already encoded so there was no need to re-encode it using json_encode($data).

I’m not familiar with Guzzle but by simply running my original code and changing the following it worked perfectly:

$postdata = json_encode($data);

to

$postdata = $data;

or more streamlined:

// $postdata = json_encode($data);
// $postdata = $data;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

I hope this helps out fellow newbies!

Many thanks,
Greg