14 Replies Latest reply on Jul 18, 2016 9:46 PM by Liam Dilley

    How to submit form data to 3rd party REST API service

    SmilingGoat

      Hi Guys,

       

      This is new to me, so any sample scripts and procedures will be appreciated.

       

      Problem:

      My client has opted for a more advanced 3rd party CRM solution which uses REST API

      They want form data captured (Contact us form) and submitted to the 3rd party CRM

       

      The CRM API sample scripts available are PHP and makes no sense to me.

      This is their sample form and php script.

       

      Questions:

      1) How do I convert this to work in BC

      2) Is it possible to use Liquid as a server side script to do API calls?

      3) Also would like to hide the API user name and token?

       

      Samples:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html>

        <head>

          <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

          <style type="text/css">

            label, input, textarea { display: block; float: left; width: 150px; margin: 5px; }

            label { clear: left; text-align: right; }

            input[type="submit"] { width: 60px; margin: 10px 0 0 165px; clear: left; }

          </style>

        </head>

        <body>

          <h2>Contact Us</h2>

          <!-- REQUIRED Edit with the filename of the script if different than the example -->

          <form action="Solve360ContactSave.php" method="GET">

            <label>First name (required)</label>

            <input type="text" name="firstname" value="" />

            <label>Last Name</label>

            <input type="text" name="lastname" value="" />

            <label>Job title</label>

            <input type="text" name="jobtitle" value="" />

            <label>Business email</label>

            <input type="text" name="businessemail" value="" />

            <label>Note</label>

            <textarea name="note" cols="4" rows="4"></textarea>

            <input type="submit" value="Save" />

          </form>

        </body>

      </html>

       

       

      PHP:

      <?php

       

       

          // version 2.0

       

       

          // All placeholders such as {ownership}, {categoryId}, {templateId} should

          // be replaced with real values without the {} brackets

       

       

          // REQUIRED Edit with your email address

          define('USER', 'xxxxxxxx@xxxxxxxx.com');

          // REQUIRED Edit with token, Workspace > My Account > API Reference > API Token

          define('TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 

       

       

          // Get request data

          $requestData = array();

          parse_str($_SERVER['QUERY_STRING'], $requestData);

       

       

          // Configure service gateway object

          require 'Solve360Service.php';

          $solve360Service = new Solve360Service(USER, TOKEN);

       

       

          //

          // Adding the contact

          //

       

       

          $contactData = array(

              'firstname'     => $requestData['firstname'],

              'lastname'      => $requestData['lastname'],

              'jobtitle'      => $requestData['jobtitle'],

              'businessemail' => $requestData['businessemail'],

       

       

              // OPTION Apply category tag(s) and set the owner for the contact to a group

              // You will find a list of IDs for your tags, groups and users in Workspace > My Account > API Reference

              // To enable this option, uncomment the following:

       

       

              /*       

              // Specify a different ownership i.e. share the item

              'ownership'     => {ownership},

       

       

              // Add categories

              'categories'    => array(

                  'add' => array('category' => array({categoryId},{categoryId}))

              ),

              */       

             

          );

         

          $contact = $solve360Service->addContact($contactData);

         

          if (isset($contact->errors)) {

              // Mail yourself if errors occur 

              mail(

                  USER,

                  'Error while adding contact to Solve',

                  'Error: ' . $contact->errors->asXml()

              );

              die ('System error');

          } else {

              // Get new contact params from the response

              $contactName = (string) $contact->item->name;

              $contactId   = (integer) $contact->item->id;

             

              // Mail yourself the result

              mail(

                  USER,

                  'New contact added to Solve',

                  'New contact "' . $contactName . '" with id ' . $contactId . ' was added to Solve360'

              );

          }

         

          //

          // OPTION Adding a activity

          //

         

          /*

           * You can attach an activity to the contact you just created

           * This example creates a Note, to enable this feature just uncomment the following request

           */   

         

          /*   

          // Preparing data for the note

          $noteData = array(

              'details' => nl2br($requestData['note'])

          );

       

       

          $note = $solve360Service->addActivity($contactId, 'note', $noteData);

         

          // Mail yourself the result

          mail(

              USER,

              'Note was added to "' . $contactName . '" contact in Solve',

              'Note with id ' . $note->id . ' was added to the contact with id ' . $contactId

          );

          // End of adding note activity

          */

       

          //

          // OPTION Inserting a template of activities

          //

         

          /*

           * You can also insert a template directly into the contact you just created

           * You will find a list of IDs for your templates in Workspace > My Account > API Reference

           * To enable this feature just uncomment the following request

           */

       

       

          /*

          // Start of template request

          $templateId = {templateId};

          $template = $solve360Service->addActivity($contactId, 'template', array('templateid' => $templateId));

             

          // Mail yourself the result

          mail(

              USER,

              'Template was added to "' . $contactName . '" contact in Solve',

              'Template with id ' . $templateId . ' was added to the contact with id ' . $contactId

          );

          // End of template request

          */

       

       

      ?>

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html>

          <head>

              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

          </head>

          <body>

              <h2>Result</h2>

              <p>Thank you, <b><?php echo $contactName ?></b></p>

              <p>Your data was successfully saved.</p>

          </body>

      </html>