-
Wufoo, Flash and PHP
hey guys :)
i just signed up for wufoo - what seems to be a really awesome free online database service! i want to use it for a couple of simple things in my online christmas card this year :D
but have run into trouble hooking up their API. they have pretty good documentation for if you want to make your own custom html forms and hook them up to the database, but i want to communicate from flash (something i don't have a great deal of experience with on the PHP side of things) and am getting nowhere. would love it if someone could take a look at my code and let me know where i'm going wrong...
first of all the AS3:
Code:
private function submitted(e:XmasEvent):void
{
var variables = new URLVariables();
variables.Field1 = e.arg[0];
variables.Field5 = e.arg[2];
variables.Field3 = e.arg[1];
var _request:URLRequest = new URLRequest("php/submit.php");
_request.data = variables;
trace(variables);
_request.method = URLRequestMethod.POST;
var _loader:URLLoader = new URLLoader()
_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
_loader.addEventListener(Event.COMPLETE, sendComplete);
_loader.addEventListener(IOErrorEvent.IO_ERROR, sendIOError);
_loader.load(_request);
_fillIn.removeEventListener(XmasEvent.SUBMIT_CLICK, submitted);
}
private function sendIOError(e:IOErrorEvent):void
{
trace("doh");
}
public function sendComplete (event:Event):void
{
trace(event.target.data);
}
ok, here's the variables trace:
Field1=em&Field3=%0Dtest&Field5=20
(this is my wufoo form data from their site:
API ID Field Title
1 Name
5 Number
3 Message)
and here's my crazy trace from the sendComplete function:
%3C%3Fphp%0A%2F%2Fif%20the%20form%20was%20submitte d%20run%20the%20API%20scripts%0Aif%20%28count%28%2 4%5FPOST%29%20%3E%200%29%20%7B%0A%09%24fieldErrors %20=%20initAPI%28%24root%29%3B%0A%7D%0A%0A%0Afunct ion%20initAPI%28%24root%29%7B%0A%09require%5Fonce% 28%24root%2E%27json%2Ephp%27%29%3B%0A%09%24wufoo%5 Fquery%5Fvals%20%3D%20setPostParams%28%29%3B%0A%09 %24request%20%3D%20generatePost%28%24wufoo%5Fquery %5Fvals%29%3B%0A%09%24response%20%3D%20runCurl%28% 24request%29%3B%0A%09%24wufoo%5Fresponse%20%3D%20g etFormJson%28%24response%29%3B%0A%09%0A%09if%28%21 success%28%24wufoo%5Fresponse%29%29%7B%0A%09%09%24 fieldErrors%20%3D%20buildErrors%28%24wufoo%5Frespo nse%29%3B%0A%09%09return%20%24fieldErrors%3B%0A%09 %7D%0A%7D%0A%09%0A%2F%2F%20Set%20the%20Wufoo%20Que ry%20POST%20parameters%0Afunction%20setPostParams% 28%29%7B%0A%09%24wufoo%5Fquery%5Fvals%20%3D%20arra y%28%0A%20%20%20%20%09%27w%5Fapi%5Fkey%27%20%3D%3E %20%2773IW%2DTF7L%2DAMXU%2DBUMS%27%2C%0A%20%20%20% 20%09%27w%5Fform%27%20%3D%3E%20%27egon%27%2C%0A%0A %09%09%271%27%20%3D%3E%20%24%5FPOST%5B%27Field1%27 %5D%2C%0A%09%09%275%27%20%3D%3E%20%24%5FPOST%5B%27 Field5%27%5D%2C%0A%09%09%273%27%20%3D%3E%20%24%5FP OST%5B%27Field3%27%5D%0A%09%29%3B%0A%09return%20%2 4wufoo%5Fquery%5Fvals%3B%0A%7D%0A%0A%2F%2F%20Gener ate%20the%20POST%20string%0Afunction%20generatePos t%28%24wufoo%5Fquery%5Fvals%29%7B%0A%09foreach%28% 24wufoo%5Fquery%5Fvals%20as%20%24key%20%3D%3E%20%2 4value%29%20%7B%0A%20%20%20%20%09%24request%20%2E% 3D%20%24key%2E%27%3D%27%2Eurlencode%28%24value%29% 2E%27&%27%3B%0A%09%7D%0A%0A%09%2F%2F%20Chop%20of%2 0the%20trailing%20ampersand%0A%09%24request%20=%20 rtrim%28%24request%2C%20%27&%27%29%3B%0A%09return% 20%24request%3B%0A%7D%0A%0A%2F%2F%20run%20the%20cu rl%20functions%0Afunction%20runCurl%28%24request%2 9%7B%0A%09%24ch%20=%20curl%5Finit%28%22http%3A%2F% 2Fzoobuffalo%2Ewufoo%2Ecom%2Fapi%2Finsert%2F%22%29 %3B%20%0A%09curl%5Fsetopt%28%24ch%2C%20CURLOPT%5FR ETURNTRANSFER%2C%201%29%3B%0A%09curl%5Fsetopt%28%2 4ch%2C%20CURLOPT%5FPOST%2C%201%29%3B%0A%09curl%5Fs etopt%28%24ch%2C%20CURLOPT%5FPOSTFIELDS%2C%20%24re quest%29%3B%0A%0A%09%2F%2F%24response%20is%20a%20J SON%20string%20returned%20from%20Wufoo%27s%20serve rs%0A%09%24response%20%3D%20curl%5Fexec%28%24ch%29 %3B%0A%09curl%5Fclose%20%28%24ch%29%3B%09%0A%09ret urn%20%24response%3B%0A%7D%0A%0A%2F%2Ftakes%20in%2 0a%20json%20string%20and%20turns%20it%20into%20an% 20object%0Afunction%20getFormJson%28%24response%29 %20%7B%0A%09%24json%20%3D%20new%20Services%5FJSON% 28%29%3B%0A%09%24input%20%3D%20%24response%3B%0A%0 9%24g%5Fform%20%3D%20%24json%2D%3Edecode%28%24inpu t%29%3B%0A%09return%20%24g%5Fform%3B%0A%7D%0A%0A%2 F%2Fcheck%20if%20the%20submission%20was%20a%20succ ess%0Afunction%20success%28%24wufoo%5Fresponse%29% 7B%0A%09%24success%20%3D%20%24wufoo%5Fresponse%2D% 3Ewufoo%5Fsubmit%5B0%5D%2D%3Esuccess%3B%0A%09if%20 %28%24success%20%3D%3D%20%22true%22%29%20echo%20%2 2Successful%20Submission%21%22%3B%0A%09else%20retu rn%20false%3B%0A%7D%0A%0A%2F%2Fbuild%20the%20error s%20array%0A%2F%2Fbuild%20the%20errors%20array%0Af unction%20buildErrors%28%24wufoo%5Fresponse%29%7B% 0A%09%2F%2Fdetermine%20how%20many%20errors%20were% 20returned%0A%09%24errorCount%20%3D%20count%28%24w ufoo%5Fresponse%2D%3Ewufoo%5Fsubmit%5B0%5D%2D%3Efi eld%5Ferrors%29%3B%0A%09%24fieldErrors%20%3D%20arr ay%28%29%3B%0A%09%24response%20%3D%20%24wufoo%5Fre sponse%2D%3Ewufoo%5Fsubmit%5B0%5D%2D%3Efield%5Ferr ors%3B%0A%09%0A%09%2F%2Fpopulate%20the%20array%20w ith%20fieldid%2D%3Eerror%20message%20pairs%0A%09fo r%28%24i%20%3D%200%3B%20%24i%3C%24errorCount%3B%20 %24i%20%20%20%29%7B%0A%09%09%24fieldErrors%5B%24re sponse%5B%24i%5D%2D%3Efield%5Fid%5D%20%3D%20%24res ponse%5B%24i%5D%2D%3Eerror%5Fmessage%3B%0A%09%7D%0 A%09return%20%24fieldErrors%3B%0A%7D%0A%0A%2F%2Ffu nction%20to%20add%20error%20messages%20wrapped%20i n%20%3Cp%3E%20tags%20or%20an%20error%20class%0Afun ction%20addErrors%28%24fieldName%2C%20%24fieldErro rs%2C%20%24add%29%7B%0A%09if%28%24fieldErrors%5B%2 4fieldName%5D%20%21%3D%20%27%27%29%7B%0A%09%09if%2 0%28%24add%20%3D%3D%20%27message%27%29%20return%20 %27%3Cp%20class%3D%22error%22%3E%27%2E%24fieldErro rs%5B%24fieldName%5D%2E%27%3C%2Fp%3E%27%3B%0A%09%0 9else%20if%20%28%24add%20%3D%3D%20%27class%27%29%2 0return%20%27class%20%3D%22error%22%27%3B%0A%09%7D %0A%7D%0A%0A%3F%3E
...
eek! i can't read it! ok so onto the php:
Code:
<?php
//if the form was submitted run the API scripts
if (count($_POST) > 0) {
$fieldErrors = initAPI($root);
}
function initAPI($root){
require_once($root.'json.php');
$wufoo_query_vals = setPostParams();
$request = generatePost($wufoo_query_vals);
$response = runCurl($request);
$wufoo_response = getFormJson($response);
if(!success($wufoo_response)){
$fieldErrors = buildErrors($wufoo_response);
return $fieldErrors;
}
}
// Set the Wufoo Query POST parameters
function setPostParams(){
$wufoo_query_vals = array(
'w_api_key' => '0000-0000-0000-0000',
'w_form' => 'egon',
'1' => $_POST['Field1'],
'5' => $_POST['Field5'],
'3' => $_POST['Field3']
);
return $wufoo_query_vals;
}
// Generate the POST string
function generatePost($wufoo_query_vals){
foreach($wufoo_query_vals as $key => $value) {
$request .= $key.'='.urlencode($value).'&';
}
// Chop of the trailing ampersand
$request = rtrim($request, '&');
return $request;
}
// run the curl functions
function runCurl($request){
$ch = curl_init("http://zoobuffalo.wufoo.com/api/insert/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
//$response is a JSON string returned from Wufoo's servers
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
//takes in a json string and turns it into an object
function getFormJson($response) {
$json = new Services_JSON();
$input = $response;
$g_form = $json->decode($input);
return $g_form;
}
//check if the submission was a success
function success($wufoo_response){
$success = $wufoo_response->wufoo_submit[0]->success;
if ($success == "true") echo "Successful Submission!";
else return false;
}
//build the errors array
//build the errors array
function buildErrors($wufoo_response){
//determine how many errors were returned
$errorCount = count($wufoo_response->wufoo_submit[0]->field_errors);
$fieldErrors = array();
$response = $wufoo_response->wufoo_submit[0]->field_errors;
//populate the array with fieldid->error message pairs
for($i = 0; $i<$errorCount; $i++ ){
$fieldErrors[$response[$i]->field_id] = $response[$i]->error_message;
}
return $fieldErrors;
}
//function to add error messages wrapped in <p> tags or an error class
function addErrors($fieldName, $fieldErrors, $add){
if($fieldErrors[$fieldName] != ''){
if ($add == 'message') return '<p class="error">'.$fieldErrors[$fieldName].'</p>';
else if ($add == 'class') return 'class ="error"';
}
}
?>
apologies for the long post. I have made the API key 000-000-000-000 but in my version am using my own correct key. anyone able to spot my error here??
thanks in advance :)
emma.
-
Hi,
have you ever seen a "character table" on your computer. They may be labelled in different ways - hopefully you find one where the space character is on row labelled 2 and column labelled 0. So the %20 is a space character, and you will recognize the 3C as a <, the 3F as ?, and so on. For whatever reason you get the php script back rather than results from it
Musicman