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

Leave a Reply

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