PDA

View Full Version : http_build_query() and URLRequest - an annoying problem



Kloucek
May 14th, 2009, 01:30 PM
Hi,

I'm working to gain some proficiency in communicating flash and php. I recently found this nice method to feed variables from php into flash.

I'm using http_build_query() function in a php file to send some variables like this:

$users = mysql_query("SELECT * FROM users WHERE uid=666");
$ua = mysql_fetch_array($users);

$vars = array();

$vars['user_id'] = $ua[0]; //should be 666
$vars['pet_id'] = $ua[1]; //should be 777
$vars['cash'] = $ua[2]; //should be 120
$vars['advicepoints'] = $ua[3]; //should be 5
$vars['AP'] = $ua[4]; //ahould be 100
$vars['timestamp'] = $ua[5]; //should be 1242291847078
$vars['income'] = $ua[6]; //should be 24
$vars['attractiveness']= $ua[7]; //should be 14

$returnString = http_build_query($vars);
echo $returnString;
It gives a nice and expected output in the browser:

user_id=666&pet_id=777&cash=120&advicepoints=5&AP=100&timestamp=1242291847078&income=24&attractiveness=14Then in flash I grab those variables using:

var request:URLRequest = new URLRequest("http://localhost/~sandbox/send_data.php");
request.method = URLRequestMethod.GET;

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.load(request);

function completeHandler(evt:Event) {

trace (evt.target.data);


}
The output of trace is not so obvious and expected (at least to me):


advice%5Fpoints=5&AP=100&income=24&timestamp=1242291847078&pet%5Fid=777&attractiveness=14&cash=120&%20uid=666First of all the order of the output is a mystery to me. It's not in alphabetical order, it's not in numerical order and FIRST OF ALL it's not in the order i've put it :/ (but that also happened to me when i was trying out multiple flashVars, so i just assume, that's just Adobe idea for a joke.. ;) ).

EDIT - oh, i've noticed, that this order IS vurnerable to changes. However those are equally uncomprehensible to me as well

Second - the misterious "%20" (space?!?) before uid var. Now how did you get there "%20" and why do i get the feeling you are my problem?!?

And finally when i trace all the variables evt.target.data.<var name> only uid comes as undefined...

And the last sentence more or less wraps up my problem. How the heck can i trace uid(and later do more unmoral things with it)?

Oh please, oh please help me kirupa forum :)

DfKimera
May 14th, 2009, 04:48 PM
You can try calling urldecode() to turn the %-based entities back to their original forms.

wvxvw
May 14th, 2009, 06:47 PM
>> It gives a nice and expected output in the browser:
Well, you're wrong... it's UNexpected output, underscore isn't a valid symbol of URIComponent.

Kloucek
May 15th, 2009, 02:15 AM
:wvxvw

I thought about it and i tried to get rid of all underscores. Well, not a solution to this problem

wvxvw
May 15th, 2009, 04:50 AM
Don't use URLLoaderDataFormat.VARIABLES, use URLLoaderDataFormat.TEXT, and format the url as you wish, but if you put there underscores, standard browsers will not display your page / will not send the request properly. :/