The weeks are passing faster than I would like. But hey, as long as we’re having fun, let the weeks keep passing.
Before we know it, Thanksgiving and Christmas will be here and it’ll be the end of the year.
Nevertheless, last week’s tutorial was about how to integrate CCB and Mailchimp using Mailchimp’s API 3.0 and PHP.
Moving right along, I’m reshuffling a few tutorials and leap frogging a couple for the remainder of the year.
Today’s tutorial focuses on how to access Wufoo forms using the Wufoo API and PHP.
Although CCB provides a somewhat user friendly product, forms are a bit complicated to customize when it comes to their look and feel.
Often times you must hijack the DOM and inject DOM elements with customized styling and etc.
And anytime I use the word “hijack”, just know it’s NOT RECOMMENDED.
Yes, hijacking the DOM works… but just because you can do it, doesn’t mean you should. 😉
People often opt to use such online form services as Wufoo and Typeform (will cover in a week or so) because of their sleek looking and customizable form designs.
But then spend countless hours manually migrating data from Wufoo or Typeform into CCB, if at all.
Today I want to share how you, YES YOU, can programmatically access your Wufoo form data using Wufoo’s API and a bit of PHP.
I’m not covering the full integration of Wufoo and CCB today, but I will in the coming month. So, sit tight and be patient.
Let’s get started with today’s tutorial.
Your Wufoo Account and Form
For starters, if you haven’t already created a Wufoo account and your first form, please do so now. This is the form for today’s tutorial.
It’s a simple form to collect the following fields:
- First Name
- Last Name
- Phone
We’ll use the Wufoo API to access your form data. In addition, Wufoo also offers a PHP wrapper too. I’ll cover that in the coming weeks.
For now, go ahead and complete the form a few times so that you have data to access.
Setting Wufoo API and PHP variables
If you plan on integrating today’s tutorial with CCB data, then I suggest you start by including the normal files inclusions and comments as we have in previous tutorials.
Next, using the Wufoo API documentation, define the following variables:
- wApiKey – set this variable equal to the Wufoo API key
- wUID – set this variable equal to the Wufoo form Id
- wBaseAPIUrl – set this variable equal to the Wufoo base url for forms
<?PHP // WUFOO TEST FORM // https://ccbtutorials.wufoo.com/forms/zjxvprb0k33gbm/ /* SYNCING CCB PROFILE DATA TO WUFOO Simple tutorial to add CCB profiles from Wufoo survey(s) */ // contains resusable globals and functions include("includes/config.php"); // Wufoo Credentials // set the Wufoo API key - read About API: wufoo.github.io/docs/ $wApiKey = 'ZZZZ-ZZZZ-ZZZZ-ZZZZ'; // set the id of desired Wufoo form ID $wUID = 'zxzxzxzxzxzxzx'; // set the base API URL $wBaseAPIUrl = 'https://<YourWuffooUsername>.wufoo.com/api/v3/forms/'; ?>
Time to make Wufoo API call using PHP
After setup credentials have been successfully defined, it’s time to create the target url that will be passed to the Wufoo function to make the API call.
Create a targetUrl variable and set the value to look like the code below:
<?PHP // set taret url $targetUrl = $wBaseAPIUrl.$wUID.'/entries.json?sort=EntryId&sortDirection=ASC'; ?>
Note: the Wufoo API can return data using json or xml. Read the Wufoo API for more details.
Next, create a responseData variable and set it equal to the Wufoo function, passing the targetUrl as the function argument.
<?PHP // retrieve form entries $responseData = Wufoo($targetUrl); ?>
I’m not going into the Wufoo function details, but will include it at the end of this tutorial.
Parsing Wufoo API and PHP response
Next, set responseData variable again. This time set it to use PHP’s built-in json_decode method, setting the first argument to pass the previous responseData variable and the second argument to true.
To parse each form entry and its respective fields, use nested foreach statements.
The first (most outer) foreach statement parses the responseData for each completed form while the inner foreach statement parses each form field for each position.
Within the inner most foreach statement, use a if statement to string match (PHP’s strpos method) the key value for each field entry, matching ‘Field’ as the string.
Within the if statement, echo the form’s key field name and values respectively.
Close both foreach statements, and it’s time to test.
<?PHP // json decode response data $responseData = json_decode($responseData,true); // parse each form entry and its data fields foreach ($responseData['Entries'] as $value) { echo '<h3>Record '.$value['EntryId'].'</h3>'; //parse each entry's respective fields foreach($value as $key => $data){ // string match key for the string value 'Field' //if (preg_match('field',$key)) if (strpos($key, 'Field') !== false) echo $key.' - '.$data.'<br>'; //} } } ?>
Put it together and test away
Yes, you’re ready to test your Wufoo API prowess and gain a quite a trove of manual migration hours back.
Save your file and open it via a web browser. Here’s what your code should look like:
<?PHP // WUFOO TEST FORM // https://ccbtutorials.wufoo.com/forms/zjxvprb0k33gbm/ /* SYNCING CCB PROFILE DATA TO WUFOO Simple tutorial to add CCB profiles from Wufoo survey(s) */ // contains resusable globals and functions include("includes/config.php"); // Wufoo Credentials // set the Wufoo API key - read About API: wufoo.github.io/docs/ $wApiKey = 'ZZZZ-ZZZZ-ZZZZ-ZZZZ'; // set the id of desired Wufoo form ID $wUID = 'zxzxzxzxzxzxzx'; // set the base API URL $wBaseAPIUrl = 'https://<YourWuffooUsername>.wufoo.com/api/v3/forms/'; // set taret url $targetUrl = $wBaseAPIUrl.$wUID.'/entries.json?sort=EntryId&sortDirection=ASC'; // retrieve form entries $responseData = Wufoo($targetUrl); // json decode response data $responseData = json_decode($responseData,true); // parse each form entry and its data fields foreach ($responseData['Entries'] as $value) { echo '<h3>Record '.$value['EntryId'].'</h3>'; //parse each entry's respective fields foreach($value as $key => $data){ // string match key for the string value 'Field' //if (preg_match('field',$key)) if (strpos($key, 'Field') !== false) echo $key.' - '.$data.'<br>'; //} } } ?>
With a bit of elbow grease and studying previous tutorials, you’ll have Wufoo form data integrated to CCB in no time.
Stay tuned, next week I’m coming how to access Typeform data for those of you that use Typeform for your web forms.
See you next week.
Wufoo Function
If you’re integrating the Wufoo function with CCB, then don’t forget to include the code below in the general.php file.
<?PHP /***************************************************************** *** Function to retrieve data from Wufoo using curl and API ******************************************************************/ function Wufoo($destUrl){ global $wApiKey; $passToken = "$wApiKey:footastic"; $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,5); curl_setopt($curl, CURLOPT_PORT, 443); curl_setopt($curl, CURLOPT_USERPWD, $passToken); //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2); curl_setopt($curl, CURLOPT_URL, $destUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Sample Code'); $response = curl_exec($curl); $resultStatus = curl_getinfo($curl); if($resultStatus['http_code'] == 200) { //$json = json_decode($response); //return json_encode($json, JSON_PRETTY_PRINT); return $response; } else { return 'Call Failed '.print_r($resultStatus); } } ?>