123FormBuilder API – PHP Script Example

This script performs the following steps:

  • Step 1: retrieves all forms in the given account (determined by the API key)
  • Step 2: determines the first form in the account
  • Step 3: counts submissions for that form
  • Step 4: prints the last 200 submissions, divided into groups of 50
  • Step 5: posts a new webhook to that form
  • Step 6: gets all forms belonging to a subuser by his/her account e-mail address
  1. <?php
  2.  $apiKey="your-api-key-here"; //replace it with your own API key from "My Account" page
  3.  $curl_post_string="apiKey=".$apiKey;
  4.  
  5.  
  6.  $url="https://www.123formbuilder.com/api/forms.xml";
  7.  $user_forms_xml=getCurlContentString($url, $curl_post_string);
  8.  
  9.  $doc = new DOMDocument();
  10.  $doc->loadXML($user_forms_xml);
  11.  
  12.  $root = $doc->documentElement;
  13.  if ($root->nodeName=="errorMessage") {
  14.   die($root->nodeName.": ".$root->nodeValue);
  15.  }
  16.  
  17.  
  18.  $firstFormXml="";
  19.  foreach ($root->childNodes as $child) {
  20.   if ($child->nodeType == 1) {
  21.    $firstFormXml=$child;
  22.    break;
  23.   }
  24.  }
  25.  
  26.  $firstForm=array();
  27.  if (!empty($firstFormXml)) {
  28.   foreach ($firstFormXml->childNodes as $fieldNode) {
  29.    if ($fieldNode->nodeType == 1) {
  30.     if ($fieldNode->nodeName == "formEmails") { //<formEmails> has <email> children
  31.      $firstForm[$fieldNode->nodeName]=array();
  32.      $emails_array=array();
  33.      foreach ($fieldNode->childNodes as $sub_child) {
  34.       $sub_child_value=trim($sub_child->nodeValue);
  35.       if (!empty($sub_child_value)) {
  36.        $emails_array[] = $sub_child_value;
  37.       }
  38.      }
  39.      $firstForm[$fieldNode->nodeName]=$emails_array;
  40.     } else {
  41.      $firstForm[$fieldNode->nodeName] = $fieldNode->nodeValue;
  42.     }
  43.    }
  44.   }
  45.  }
  46.  
  47.  echo "First form:";
  48.  print_r($firstForm);
  49.  
  50.  
  51.  $firstFormId=$firstForm["formId"];
  52.  $url="https://www.123formbuilder.com/api/forms/$firstFormId/submissions/count.xml";
  53.  $submissions_count_xml=getCurlContentString($url, $curl_post_string);
  54.  
  55.  $doc = new DOMDocument();
  56.  $doc->loadXML($submissions_count_xml);
  57.  
  58.  $root = $doc->documentElement;
  59.  
  60.  if ($root->nodeName=="errorMessage") {
  61.   die($root->nodeName.": ".$root->nodeValue);
  62.  }
  63.  $submissionsCount="";
  64.  foreach ($root->childNodes as $child) { //1
  65.   if ($child->nodeType == 3) { //we make sure that it's a text node
  66.    $submissionsCount=$child->nodeValue;
  67.    break;
  68.   }
  69.  }
  70.  echo "<br /><br />";
  71.  echo "Number of submissions: ".$submissionsCount;
  72.  
  73.  
  74.  $pageNrArray=array(0,1,2,3);
  75.  $pageSize=50;
  76.  $last_200_submissions=array();
  77.  foreach ($pageNrArray as $key=>$pageNr) {
  78.   $url="https://www.123formbuilder.com/api/forms/$firstFormId/submissions.xml";
  79.   $submissions_xml=getCurlContentString($url,
  80.    $curl_post_string."&pageNr=$pageNr&pageSize=$pageSize&sort=DESC");
  81.  
  82.   $doc = new DOMDocument();
  83.   $doc->loadXML($submissions_xml);
  84.  
  85.   $root = $doc->documentElement;
  86.  
  87.   if ($root->nodeName=="errorMessage") {
  88.    die($root->nodeName.": ".$root->nodeValue);
  89.   }
  90.  
  91.   foreach ($root->childNodes as $child) {
  92.    //we make sure that the node is an element (not an attribute or a text node):
  93.    if ($child->nodeType == 1 && $child->nodeName == "submission") {
  94.     $new_submission=array();
  95.     $new_submission_fields=array();
  96.     foreach ($child->childNodes as $sub_child) {
  97.      if ($sub_child->nodeType==1) {
  98.       foreach ($sub_child->childNodes as $sub_node) {
  99.        if ($sub_node->nodeType==3) {
  100.         $new_submission[$sub_child->nodeName]=$sub_node->nodeValue;
  101.        } elseif ($sub_node->nodeType==1 && $sub_node->nodeName=="field") {
  102.         foreach ($sub_node->childNodes as $child_node) {
  103.          if ($child_node->nodeType==1) {
  104.           $field_name=$child_node->nodeName;
  105.           $field_value=$child_node->nodeValue;
  106.           $new_submission_field[$field_name]=$field_value;
  107.          }
  108.         }
  109.         $new_submission_fields[]=$new_submission_field;
  110.        }
  111.       }
  112.      }
  113.     }
  114.     $new_submission["fields"] = $new_submission_fields;
  115.     $last_200_submissions[] = $new_submission;
  116.    }
  117.   }
  118.  }
  119.  echo "<br /><br /><br />";
  120.  echo "Last 200 submissions:";
  121.  print_r($last_200_submissions);
  122.  
  123.  
  124.  $url="https://www.123formbuilder.com/api/forms/$firstFormId/webhooks.xml";
  125.  $webhook_url="http://www.google.com"; //replace google.com with the desired webhook URL
  126.  $post_webhook_xml=getCurlContentString($url, $curl_post_string."&webhookUrl=".
  127.   urlencode($webhook_url));
  128.  
  129.  echo "<br /><br /><br />";
  130.  echo "Posting a webhook to the form (URL: http://www.google.com)…";
  131.  
  132.  $doc = new DOMDocument();
  133.  $doc->loadXML($post_webhook_xml);
  134.  
  135.  $root = $doc->documentElement;
  136.  
  137.  if ($root->nodeName=="errorMessage") {
  138.   echo "<br /><br />";
  139.   die($root->nodeName.": ".$root->nodeValue);
  140.  }
  141.  $message="";
  142.  foreach ($root->childNodes as $child) { //1
  143.   if ($child->nodeType == 3) { //we make sure that it's a text node
  144.    $message=$child->nodeValue;
  145.    break;
  146.   }
  147.  }
  148.  echo "<br /><br />";
  149.  echo "Message: ".$message;
  150.  
  151.  
  152.  $subuserEmail="subuser-email-address-here";
  153.  $subuserEmailEncoded=urlencode($subuserEmail);
  154.  $url="https://www.123formbuilder.com/api/subusers/$subuserEmailEncoded/forms.xml";
  155.  $user_forms_xml=getCurlContentString($url, $curl_post_string);
  156.  
  157.  $doc = new DOMDocument();
  158.  $doc->loadXML($user_forms_xml);
  159.  
  160.  $root = $doc->documentElement;
  161.  if ($root->nodeName=="errorMessage") {
  162.   die($root->nodeName.": ".$root->nodeValue);
  163.  }
  164.  
  165.  $subuserForms=array();
  166.  if (!empty($root->childNodes)) {
  167.   foreach ($root->childNodes as $formNode) {
  168.    $subuserFormArray=array();
  169.    if ($formNode->nodeType == 1) {
  170.     foreach ($formNode->childNodes as $fieldNode) {
  171.      if ($fieldNode->nodeType == 1) {
  172.       if ($fieldNode->nodeName == "formEmails") { //<formEmails> has <email> children
  173.        $subuserFormArray[$fieldNode->nodeName]=array();
  174.        $emails_array=array();
  175.        foreach ($fieldNode->childNodes as $sub_child) {
  176.         $sub_child_value=trim($sub_child->nodeValue);
  177.         if (!empty($sub_child_value)) {
  178.          $emails_array[] = $sub_child_value;
  179.         }
  180.        }
  181.        $subuserFormArray[$fieldNode->nodeName]=$emails_array;
  182.       } else {
  183.        $subuserFormArray[$fieldNode->nodeName] = $fieldNode->nodeValue;
  184.       }
  185.      }
  186.     }
  187.    }
  188.    if (!empty($subuserFormArray)) {
  189.     $subuserForms[]=$subuserFormArray;
  190.    }
  191.   }
  192.  }
  193.  
  194.  echo "<br /><br /><br />";
  195.  echo "Forms of subuser with e-mail $subuserEmail:";
  196.  print_r($subuserForms);
  197.  
  198.  //function used to make an API call
  199.  function getCurlContentString($url, $curl_post_string) {
  200.   $ch = curl_init($url);
  201.   $fp = fopen("script_api_cache.txt", "w");
  202.   curl_setopt($ch, CURLOPT_FILE, $fp);
  203.   curl_setopt($ch, CURLOPT_HEADER, 0);
  204.  
  205.   curl_setopt($ch, CURLOPT_POST, 1);
  206.   curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_string);
  207.  
  208.   curl_exec($ch);
  209.   curl_close($ch);
  210.   fclose($fp);
  211.  
  212.   $page_string = file_get_contents("script_api_cache.txt");
  213.  
  214.   return $page_string;
  215.  }
  216. ?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This website is using cookies. By continuing to browse, you are
agreeing to our use of cookies as explained in our Cookie Policy. OK