Trying to create a formit custom webhook using curl

Hi,

I am very not skilled with custom integration of APIs, and I am trying to set up a custom webhook for a formit form to connect to their CRM. Would anyone be able to point me in the right direction. This is what I have figured out so far.

<?php
$formit =& $hook->formit;
$allFormFields = $hook->getValues();
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://sandbox.sherpacrm.com/',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "vendorName": "Village Website",
    "sourceCategory": "Company Website",
    "sourceName": "Company Website",
    "sourceProspectId": "3045",
    "sourceCommunityId": 4321,
    "referralDateTime": "2019-02-28T18:23:18+0:00",
    "advisorFirstName": "Bill",
    "advisorLastName": "Mitchell",
    "advisorEmail": "advisor@gmail.com",
    "advisorReferralNote": "This is the referral note updated",
    "advisorWorkPhone": "314-111-1111",
    "residentContactFirstName": "Bonnie",
    "residentContactLastName": "Smith",
    "residentContactAddress1": "200 Main St.",
    "residentContactCity": "Townington",
    "residentContactState": "MO",
    "residentContactPostalCode": "87654",
    "residentContactCountry": "US",
    "residentContactEmail": "resident40@gmail.com",
    "residentAge": 90,
    "residentGender": "F",
    "residentMaritalStatus": "M",
    "primaryContactResidentRelationship": "Daughter",
    "primaryContactFirstName": "Test",
    "primaryContactLastName": "Atrium Village",
    "primaryContactEmail": "primary20@gmail.com",
    "primaryContactHomePhone": "314-222-0022",
    "referralNote": "Test Comment"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
	'Authorization: Bearer API KEY'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

return true;

Thank you

Welcome to MODX. It can be difficult to debug things here sometimes so feel free to email me, wayne@alphatoro.com, I can setup a dev cloud for us to work on if you want.
In the meantime maybe a similar FormIt hook I did for Constant Contact can help you figure some steps out.
https://github.com/alpha-toro/modx-constant-contact-v3/blob/main/ConstantContactCreate_FormIt.snippet.php

Thank you for responding! The link you provided seems to not be working.

Sorry I had the repo on private. Try now.

@joedonjohn

If you still need some direction, I have some code that is very similar. I use it to post to Salesforce.

Thank you for responding extra help will be great. The issue I am currently having is getting the fields to populate the POSTFIELDS array.

<?php
$formit =& $hook->formit;
$formFields = $hook->getValues();
$first_1 = $formFields['first_1'];
$last_1 = $formFields['last_1'];
$email_1 = $formFields['email_1'];
$phone_1 = $formFields['phone_1'];
$message = $formFields['message'];



$curl = curl_init();

curl_setopt_array($curl, array(
 CURLOPT_URL => 'https://members.com',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "vendorName": "Company Website",
    "sourceCategory": "Company Website",
    "sourceName": "Company Website",
    "sourceProspectId": "NULL",
    "sourceCommunityId": "NULL",
    "referralDateTime": "2023-02-28T18:23:18+0:00",
    "advisorFirstName": "NULL",
    "advisorLastName": "NULL",
    "advisorEmail": "NULL",
    "advisorWorkPhone": "NULL",
    "residentContactFirstName": "NULL",
    "residentContactLastName": "NULL",
    "residentContactAddress1": "NULL",
    "residentContactCity": "NULL",
    "residentContactState": "NULL",
    "residentContactPostalCode": "NULL",
    "residentContactCountry": "US",
    "residentContactEmail": "NULL",
    "residentGender": "NULL",
    "residentMaritalStatus": "NULL",
    "primaryContactResidentRelationship": "NULL",
    "primaryContactFirstName": "$first_1",
    "primaryContactLastName": "$last_1",
    "primaryContactAddress1": "NULL",
    "primaryContactCity": "NULL",
    "primaryContactState": "NULL",
    "primaryContactPostalCode": "NULL",
    "primaryContactCountry": "NULL",
    "primaryContactEmail": "$email",
    "primaryContactHomePhone": "$phone_1",
    "primaryContactCellPhone": "NULL",
    "primaryContactWorkPhone": "NULL",
    "referralNote": "$message"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer '
  ),
));

$response = curl_exec($curl);
if(curl_errno($curl)){
    $modx->log(xPDO::LOG_LEVEL_ERROR, "curl_error: ".curl_error($curl), '', 'sherpa');
    //close cURL connection
    curl_close($curl);
    return false;
}else{
    //close cURL connection
    curl_close($curl);
    return true;
}

This is the formit hook that I use to submit all the form fields to the server url. You can either pass additional info in hidden form fields or update the snippet to add extra “settings” (IP address, date, etc…)

[[!FormIt?
   &hooks=`FormItJSON`
   &resultTpl=`v2Results`
   &server=`https://sandbox.sherpacrm.com/companies/{{COMPANY}}/communities/{{COMMUNITY}}/leads`
   &toPlaceholder=`submitResults`
   &clearFieldsOnSuccess=`0` 
   &successMessage=`YES!`
]]

Looks like you have it correct from the $hook->getValues() side of things.
I would setup an array like this

$data = json_encode(array(
"primaryContactFirstName" => $first_1,
"primaryContactLastName" => $last_1,
));

then send it to modx log just to debug:

$modx->log(modX::LOG_LEVEL_ERROR,'Form Hook was run: ' . $data);

Other debug is to make it the API works in something like Postman.

@joedonjohn Did you get this solved?