Skip to content

Instantly share code, notes, and snippets.

@michaelespinosa
Created November 15, 2011 00:34
Show Gist options
  • Save michaelespinosa/1365700 to your computer and use it in GitHub Desktop.
Save michaelespinosa/1365700 to your computer and use it in GitHub Desktop.
Using the v3 of the Wufoo API to retrieve all entries and store in a single array ($all_results). This is necessary because of the limit 100 entries limit that Wufoo has. Also store the results to file we don't get slow responses from the api.
<?php
function wufoo_entries() {
// Setup
$account = "ACCOUNT_NAME";
$form = "FORM_NAME";
$api_key = "YOUR-API-KEY";
$offset = 0;
$limit = 100;
$check_int = 15 * 60; // 15 minutes
$file_time = 'time_stamp.txt';
$file_results = 'results.txt';
// Check for the last update
$time = time();
$time_stamp = file_get_contents($file_time);
$refresh = ($check_int < ($time - $time_stamp)) ? TRUE : FALSE;
// if true run functions and save the results and time to file else pull the results from file
if ($refresh) {
// Initialize our results array
$all_results = array();
// How many entries are there?
$api_uri = "https://$account.wufoo.com/api/v3/forms/$form/entries/count.json";
$count = wufoo_api($api_key, $account, $form, $offset, $limit, $api_uri);
$counted = $count['EntryCount'];
// Call the api as many times as needed
for ($offset = 0; $offset <= $counted - 1; $offset = $offset + $limit) {
$results = wufoo_api($api_key, $account, $form, $offset, $limit);
// Limit the number of times we put results into the $all_results array to make sure we don't end up with blanks
$limit = (($offset + $limit) > $counted) ? $limit - (($offset + $limit) - $counted) : $limit;
// Iterate through the Entries array to push each entry into a new array
for ($result = 0; $result <= $limit - 1; $result++) {
array_push($all_results, $results['Entries'][$result]);
}
}
// Save the new results (as json) and time to file
$all_results_file = json_encode($all_results);
file_put_contents($file_results, $all_results_file);
file_put_contents($file_time, $time);
return $all_results;
}
else {
// Pull the results array from file
$all_results = file_get_contents($file_results);
$all_results = json_decode($all_results, true);
return $all_results;
}
}
function wufoo_api($api_key, $account, $form, $offset, $limit, $api_uri = NULL) {
// Check to see if it's asking for the the entries or the total count
$api_url = ($api_uri) ? $api_uri : 'https://' . $account . '.wufoo.com/api/v3/forms/' . $form . '/entries.json?pageStart=' . $offset . '&pageSize=' . $limit;
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, $api_key . ':footastic');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Wufoo Sample Code');
$response = curl_exec($curl);
$resultStatus = curl_getinfo($curl);
if ($resultStatus['http_code'] == 200) {
$results = json_decode($response, true);
return $results;
} else {
$results = 'Call Failed '.print_r($resultStatus, true);
return $results;
}
}
// Show me some results and make sure it works
echo "<pre>";
print_r(wufoo_entries());
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment