APIs & Integrations

andyhoek
Participant | Diamond Partner
Participant | Diamond Partner

Problem with Forms API and PHP

Hi everyone,

 

We're running into an issue using the Forms API with PHP. We want to connect four forms on a website to HubSpot.

 

We get this error message: Invalid input JSON on line 1, column 39: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token

 

Full error message is at the end of the code provided below:

 

Does anyone have an idea what's causing this error and how we can solve this? Thanks in advance!

 

Below is the PHP code (and his notes) that our developer has provided me with:

//////////////////////////////////////////////
Main hubspot api function.

Here we check the info to send and the kind of request (donation, crowdfunding, petition or newsletter)
//////////////////////////////////////////////

private function hubspot($form_parsed_post = array()) {

        // Default values. We check before send $form_parsed_post the data, but is double check to prevent errors.
        $request_url   = "https://hivos.nl";
        $pre_title     = "Doneer";
        $current_title = "Hivos People Unlimited";
        $hub_key       = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

        // We check if we receive the hubspot function call, from a donation, newsletter, petition or other source.
        $donation_flag     = ( isset($form_parsed_post["donation-flag"]) && $form_parsed_post["donation-flag"] === true ? true : false );
        $newsletter_flag   = ( isset($form_parsed_post["newsletter-flag"]) && $form_parsed_post["newsletter-flag"] === true ? true : false );
        $crowdfunding_flag = ( isset($form_parsed_post["crowdfunding-flag"]) && $form_parsed_post["crowdfunding-flag"] === true ? true : false );
        $petition_flag     = ( isset($form_parsed_post["petition-flag"]) && $form_parsed_post["petition-flag"] === true ? true : false );

        $fields = array();

        if ($donation_flag)
        {
            // Is donation call:

            $name = "";

            if (isset($form_parsed_post["voorletters"]))
            {
                $name = $form_parsed_post["voorletters"];
            }
            else if (isset($form_parsed_post["voornaam"]))
            {
                $name = $form_parsed_post["voornaam"];
            }

            $form_parsed_post = array(
                "email"          => $form_parsed_post["email"],
                "voornaam"       => $name,
                "achternaam"     => (isset($form_parsed_post["achternaam"]) ? $form_parsed_post["achternaam"] : false),
                "address"        => (isset($form_parsed_post["address"]) ? $form_parsed_post["address"] : false),
                "huisnummer"     => (isset($form_parsed_post["huisnummer"]) ? $form_parsed_post["huisnummer"] : false),
                "postal_code"    => (isset($form_parsed_post["postal_code"]) ? $form_parsed_post["postal_code"] : false),
                "voonplaats"     => (isset($form_parsed_post["voonplaats"]) ? $form_parsed_post["voonplaats"] : false),
                "phone"          => (isset($form_parsed_post["phone"]) ? $form_parsed_post["phone"] : false),
                "market_code"    => (isset($form_parsed_post["market_code"]) ? $form_parsed_post["market_code"] : false),
                "brq_amount"     => (isset($form_parsed_post["brq_amount"]) ? $form_parsed_post["brq_amount"] : "0"),
                "nieuwsbrief"    => (isset($form_parsed_post["nieuwsbrief"]) ? $form_parsed_post["nieuwsbrief"] : "yes"),
                "phone_agree"    => (isset($form_parsed_post["phone_agree"]) ? $form_parsed_post["phone_agree"] : false),
                "payment_period" => (isset($form_parsed_post["payment_period"]) ? $form_parsed_post["payment_period"] : "single"),
            );

            $fields = $this->get_form_parsed_post($form_parsed_post);
        }
        else if ($newsletter_flag)
        {
            // Newsletter call: firstname, lastname, email

            $hub_key   = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
            $pre_title = "Newsletter";

            $fields["email"]                 = ( isset($form_parsed_post["email"]) ? $form_parsed_post["email"] : "" );
            $fields["firstname"]             = ( isset($form_parsed_post["voornaam"]) ? $form_parsed_post["voornaam"] : "" );
            $fields["lastname"]              = ( isset($form_parsed_post["achternaam"]) ? $form_parsed_post["achternaam"] : "" );
            
        }
        else if ($crowdfunding_flag)
        {
            // Crowdfunding call: amount, firstname, lastname, email, nieuwsbrief(yes/no)
            $hub_key   = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
            $pre_title = "Crowdfunding";

            $fields["email"]       = ( isset($form_parsed_post["email"]) ? $form_parsed_post["email"] : "" );
            $fields["firstname"]   = ( isset($form_parsed_post["voorletters"]) ? $form_parsed_post["voorletters"] : "" );
            $fields["lastname"]    = ( isset($form_parsed_post["achternaam"]) ? $form_parsed_post["achternaam"] : "" );
            $fields["nieuwsbrief"] = "yes";
            $fields["amount"]      = $form_parsed_post["amount"];
            
        }
        else if ($petition_flag)
        {
            // Petition call: firstname, lastname, email, city, petition_public(yes/no)
            $hub_key   = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
            $pre_title = "Petition";
            
            /*
            $fields["email"]           = ( isset($form_parsed_post["email"]) ? $form_parsed_post["email"] : "" );
            $fields["firstname"]       = ( isset($form_parsed_post["voorletters"]) ? $form_parsed_post["voorletters"] : "" );
            $fields["lastname"]        = ( isset($form_parsed_post["achternaam"]) ? $form_parsed_post["achternaam"] : "" );
            $fields["nieuwsbrief"]     = "yes";
            $fields["city"]            = $form_parsed_post["woonplaats"];
            $fields["petition_public"] = $form_parsed_post["public_mode"];
            */
            
            // HARDCODED VALUES TO CAN CHECK QUICKLY, LIKE A MOCK.
            
            $fields["email"]           = "jose@hcpetiton.com";
            $fields["firstname"]       = "FN Jose";
            $fields["lastname"]        = "LN GS";
            $fields["city"]            = "Málaga HC";
            $fields["nieuwsbrief"]     = "yes";
            $fields["petition_public"] = "yes";
            
        }

        // We replace default URL by the current one.
        if (isset($form_parsed_post["current_url"]) && $form_parsed_post["current_url"] !== "")
        {
            $request_url = $form_parsed_post["current_url"];
        }

        if (isset($form_parsed_post["current_title"]) && $form_parsed_post["current_title"] !== "")
        {
            $current_title = $form_parsed_post["current_title"];
        }

        // We setup the final page-title string (prefix can be doneer or newsletter + page title)
        $page_title = "$pre_title - $current_title";

        //////////////////// REQUEST ///////////////////////////////////////////

        $hutspot_url = "https://api.hsforms.com/submissions/v3/integration/submit/XXXXXX/" . $hub_key;

        # compose the data object to send to hubspot
        $hutspot_object = array(
            "submittedAt" => time() * 1000,
            "fields"      => $fields,
            "context"     => array("pageUri"  => $request_url,
                "pageName" => $page_title,
            ),
        );

        # do the json
        $payload = json_encode($hutspot_object);

        //return $payload;
        
        # and off she goes!
        $ch     = curl_init($hutspot_url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
        
    }

//////////////////////////////////////////////
Request examples

//////////////////////////////////////////////

DONATION:

$form_parsed_post is an array with all user/donation data. Just before send to hubspot function we add donation-flag and other specific data for hubspot.

$form_parsed_post["donation-flag"] = true;
$form_parsed_post["current_url"]   = (isset($_POST["current_url"]) ? $_POST["current_url"] : "");
$form_parsed_post["current_title"] = (isset($_POST["current_title"]) ? $_POST["current_title"] : "");

$hub = $this->hubspot($form_parsed_post);


PETITION: 

In petition, the data is parsed in this $user_data array

$user_data = array(
    "email"         => $new_user["email"],
    "voorletters"   => $new_user["voorletters"],
    "achternaam"    => $new_user["achternaam"],
    "woonplaats"    => $new_user["woonplaats"],
    "current_title" => (isset($_POST["current_title"]) ? $_POST["current_title"] : ""),
    "current_url"   => (isset($_POST["current_url"]) ? $_POST["current_url"] : ""),
    "public_mode"   => $new_user["public_mode"],
    "petition-flag" => true
);

$result["hub"] = $this->hubspot($user_data);


NEWSLETTER:

Same with newsletter, parsed info into an array with extra hubspot specific info like newsletter-flag

$form_parsed_post = array(
    "email"           => $_POST["email"],
    "voornaam"        => ( isset($_POST["voornaam"]) ? $_POST["voornaam"] : "" ),
    "achternaam"      => ( isset($_POST["achternaam"]) ? $_POST["achternaam"] : "" ),
    "newsletter-flag" => true,
    "donatie_url"     => ( isset($_POST["current_url"]) ? $_POST["current_url"] : "" ),
    "current_title"   => ( isset($_POST["current_title"]) ? $_POST["current_title"] : "" ),
        );

$hubspot = $this->hubspot($form_parsed_post);


CROWDFUNDING:

Same flow.

$user_data = array(
    "email"             => $form_parsed_post["email"],
    "voorletters"       => $form_parsed_post["voorletters"],
    "achternaam"        => $form_parsed_post["achternaam"],
    "amount"            => $form_parsed_post["brq_amount"],
    "current_title"     => $_POST["current_title"],
    "current_url"       => $_POST["current_url"],
    "crowdfunding-flag" => true
);

$hub = $this->hubspot($user_data);


For each type, we have a specific PHP function, so is impossible that for a donation, we have crowdfunding-flag = true or other flag mess. That part works fine because for donations we see right values, but for other kind of request we get:

ie: petition with name Jose, last name Testing, email jose@testing.com, city malaga: 

{"status":"error","message":"Invalid input JSON on line 1, column 39: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token","correlationId":"02047444-07b5-4fda-ba95-b5221ca437e0"}{"post":{"action":"donatie_new_petition","email":"jose@testing.com","voorletters":"Jose","achternaam":"Testing","woonplaats":"malaga","post_id":"43335","newsletter":"yes","public_mode":"yes","current_url":"http:\/\/hivosnl.local\/donatie\/alejandra-verdedigt-mensenrechtenactivisten\/","current_title":"Alejandra"},"hub":"{\"status\":\"error\",\"message\":\"Invalid input JSON on line 1, column 39: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token\",\"correlationId\":\"02047444-07b5-4fda-ba95-b5221ca437e0\"}","return":"https:\/\/www.hivos.nl\/bedankt"}

In all cases we send $fields array with the keys provided for each case by Andy weeks ago.
I run a online marketing agency in Amsterdam: https://www.invalshoek.nl/
0 Upvotes
1 Reply 1
dennisedson
HubSpot Product Team
HubSpot Product Team

Problem with Forms API and PHP

Hi @andyhoek ,

Thanks for submitting the question.  Could you have your developer send what the JSON post body is for the form? 

I am guessing that the submitted JSON is malformed. 

It should reflect the examples here

 

Thanks!

0 Upvotes