AS3 problem with URLLoader and PHP with include

'Cause I’ve spent HOURS trying to figure this one out…here it goes. I hope my explanation isn’t too confusing…

I was trying to send some data to a PHP script and then return a few variables from it, by using URLRequest and then URLLoader to retrieve the variables from the script. The problem was that when I used

include 'config.php' 

in my script (config.php is just another script) the returned string was added a few weird characters plus some whitespaces, which made Flash return the following error:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
    at Error$/throwError()

or, if not this error, I just wasn’t able to read the variables, as they all came as “undefined”.

(I used Firebug in Firefox to see what was being passed from PHP to Flash).

I was setting up my URLLoader by using

myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

so that Flash knew that the returned data was in the form of VARIABLES (as in URLVariables…) - which makes it easy to get the values from each variable as they come in the form of name/value pairs… but this was causing the error (I think). So, my solution was to set up the URLLoader with

myLoader.dataFormat = URLLoaderDataFormat.TEXT;

to get everything as TEXT and not URLVariables and with that I managed to get all my variables in the form of a ‘single string’. From here, I could just parse the string (by using the “&” character that my php script sends along with every variable). But, instead of parsing the string, we can just convert it to URLVariables and then get the values of all the variables in a much simpler way. There’s some extra work needed to take care of the returned string - just check my code and pay attention to the comments.

Here is the complete code:



var varSend:URLRequest = new URLRequest("myscript.php"); //to test from flash, use complete path     
var variables = new URLVariables();
variables.sent = "stuff sent from flash"; = variables;
varSend.method = URLRequestMethod.POST;
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.TEXT;
myLoader.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete(e:Event){
    /* The following lines try to resolve the issue with 'include' statements in PHP scripts
    *  which add whitespaces and other characters to the return string. To solve this, the 
    *  returned data must be in the TEXT form. Then, when loaded, we "clean" the string from 
    *  the beginning to the first "&" character +1. After that, we convert the received TEXT to URLVariables.
    *  so it is easier to use (we could just parse the TEXT data instead). */
    var tempVar:String =;
    var pos = tempVar.indexOf("&");
    var finalVar = tempVar.substring(pos+1); //we HAVE TO take the first "&" out !!
    var resultVars:URLVariables = new URLVariables(finalVar);

    var phpVar1 = resultVars.var1;
    var phpVar2 = resultVars.var2;


include 'config.php';  //this is THE OTHER php script (causing the extra characters)

$sent = $_POST['sent']; //some POSTed variable from FLASH
$fromphp = "this stuff comes from php"; //some new stuff to send TO FLASH

$rString = "&var1=".$sent."&var2=".$fromphp;

echo $rString;

Well, I hope it helps! Cya!