APIs & Integrations

CHolt123
Member

integrate hubspot forms submit with laravel/guzzle

SOLVE

Hello All, hoping you can help me. I am trying to use php and guzzle to process a for m dynamically. below is my submit request:

$client = new \GuzzleHttp\Client([

            'headers' => ['Content-Type' => 'application/json']

             ]);


        $arr = [
                    [

                        "skipValidation" => true,

                        "fields" => [

                            ["name" => "firstname""value" => $request->input("first")],

                            ["name" => "lastname""value" => $request->input("last")],

                            ["name" => "phone""value" => $request->input("phone")],

                            ["name" => "email""value" => $request->input("email")],

                            ["name" => "source""value" => $request->input("LeadSource")],

                            ["name" => "ls""value" => $request->input("Lead_Source_Description__c")],

                            ["name" => "utm_medium""value" => $request->input("pLIUTMMedium")],

                            ["name" => "utm_campaign""value" => $request->input("pLIUTMCampaign")],

                            ["name" => "utm_source""value" => $request->input("pLIUTMSource")],

                            ["name" => "utm_content""value" => $request->input("pLIUTMContent")],

                            ["name" => "utm_term""value" => $request->input("pLIUTMTerm")],

                            ["name" => "adcampaign""value" => $request->input("Advertising_Campaign__c")]

                        ]

                    ]
        ];

        $post_json = json_encode($arr);
            print_r($post_json);

        
            [
                "body" =>  $post_json
            ]
        
        )->getBody();

        $result = json_decode($response);
 
The error I keep getting is:
resulted in a `400 Bad Request` response:
{"status":"error","message":"Invalid input JSON on line 1, column 1: Cannot deserialize instance of `com.hubspot.formsub 
0 Upvotes
1 Accepted solution
Teun
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

integrate hubspot forms submit with laravel/guzzle

SOLVE

Hi @CHolt123, Allthough I do not know where it goes wrong with your code, I thought it might be helpfull to share my PHP script which we use to submit to a specific form:

 

// Default settings
    $baseURL = 'https://forms.hubspot.com/uploads/form/v2/';
    // Portal ID of client
    $portalId = '';

    // Hubspot Form ID (grabbed from URL)
    $hubspot_form = '';

    $params = '';

    // Loop through all post values and build URL params, example: FirstName=Teun&LastName=Rutten
    foreach($post as $key => $value) {
      if ( ! empty( $value ) ) {
        $name = $key;
        $params = $params . $name . '=' . urlencode($value) . "&";
      }
    }

    $hubspotutk      = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : ''; //grab the cookie from the visitors browser.
    $ip_addr         = $_SERVER['REMOTE_ADDR']; //IP address too.
    
    $hs_context      = array(
      'hutk' => $hubspotutk,
      'ipAddress' => $ip_addr,
      'pageUrl' => $post['url'],
      'pageName' => isset($post['page']) ? $post['page'] : ''
    );
    $hs_context_json = json_encode($hs_context);
    $params = $params . '&hs_context=' . urlencode($hs_context_json);

    // Prepare URL
    $endpoint = $baseURL . $portalId . '/' . $hubspot_form;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response    = curl_exec($ch); //Log the response from HubSpot as needed.
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
    curl_close($ch);
    return $response . ' ' . $status_code;
  }


Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


View solution in original post

1 Reply 1
Teun
Solution
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

integrate hubspot forms submit with laravel/guzzle

SOLVE

Hi @CHolt123, Allthough I do not know where it goes wrong with your code, I thought it might be helpfull to share my PHP script which we use to submit to a specific form:

 

// Default settings
    $baseURL = 'https://forms.hubspot.com/uploads/form/v2/';
    // Portal ID of client
    $portalId = '';

    // Hubspot Form ID (grabbed from URL)
    $hubspot_form = '';

    $params = '';

    // Loop through all post values and build URL params, example: FirstName=Teun&LastName=Rutten
    foreach($post as $key => $value) {
      if ( ! empty( $value ) ) {
        $name = $key;
        $params = $params . $name . '=' . urlencode($value) . "&";
      }
    }

    $hubspotutk      = isset($_COOKIE['hubspotutk']) ? $_COOKIE['hubspotutk'] : ''; //grab the cookie from the visitors browser.
    $ip_addr         = $_SERVER['REMOTE_ADDR']; //IP address too.
    
    $hs_context      = array(
      'hutk' => $hubspotutk,
      'ipAddress' => $ip_addr,
      'pageUrl' => $post['url'],
      'pageName' => isset($post['page']) ? $post['page'] : ''
    );
    $hs_context_json = json_encode($hs_context);
    $params = $params . '&hs_context=' . urlencode($hs_context_json);

    // Prepare URL
    $endpoint = $baseURL . $portalId . '/' . $hubspot_form;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/x-www-form-urlencoded'
    ));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response    = curl_exec($ch); //Log the response from HubSpot as needed.
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Log the response status code
    curl_close($ch);
    return $response . ' ' . $status_code;
  }


Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.