6 Replies Latest reply on Aug 27, 2007 1:49 PM by latrixie

    PHP and Actionscript 3.0 issue


      I have a MySql database with some content that I want to access from my Flash movie through a PHP script. I actually get things to work (sort of) since I can pass content from the database, through the PHP script into my Flash movie - but the problem is the formatting of the content being sent to Flash. The string that I want to send is written in PHP (UTF-8 encoded) as follows:

      $file_list = array();
      array_push($file_list, "å");
      array_push($file_list, "ä");
      $temp= implode(",",$file_list);

      What I do (in this simplyfied example) is to create an array and populate it with the Swedish letters 'å' and 'ä'. These are just placeholders of course and are in my actual code retrieved from my database.

      So, "arrayen=$temp" is sent to my Flashfile, where the code looks as follows:

      ---Flash AS 3.0---
      var variables:URLVariables = new URLVariables();
      var vrequest:URLRequest = new URLRequest();
      vrequest.url = "retrieveData.php";
      vrequest.method = URLRequestMethod.POST;
      vrequest.data = variables;
      var loader:URLLoader = new URLLoader();
      loader.dataFormat = URLLoaderDataFormat.TEXT;
      loader.addEventListener(Event.COMPLETE, completeHandler);
      try {
      } catch (error:Error) {
      trace("Unable to load URL");
      function completeHandler(event:Event):void {
      var loader2:URLLoader = URLLoader(event.target);
      var variables:URLVariables = new URLVariables(loader2.data);
      The outcome of the last 'trace' should be something like


      but unfortunately it comes out as:

      %0A%0A%0Aarrayen=%C3%A5%2C%C3%A4%2C%C3%A5%2C%C3%A4%2C%C3%A5%2C%C3%A4%2C%C3%A5%2C%C3%A4%20% 0A%0A%0A%0A

      OK, I believe my problem is obvious - how on earth do I get it right?

      Thanks a milion in advance!

        • 1. Re: PHP and Actionscript 3.0 issue
          kglad Adobe Community Professional & MVP
          use the unescape() function to convert from url-encoding to ascii.
          • 2. Re: PHP and Actionscript 3.0 issue
            Level 7
            You can also use urldecode($string) in PHP.

            Dave -
            Head Developer
            Adobe Community Expert

            • 3. Re: PHP and Actionscript 3.0 issue
              latrixie Level 1
              Hi again, and thanks for your advice.

              I've done some tweaking and things are better, but with some remaining problems. No my variable output reads:


              Så, the % are gone, but the 'å' and 'ä' are still not there yet ;)

              Any help is more than welcome, thanks in advance!

              • 4. Re: PHP and Actionscript 3.0 issue
                latrixie Level 1
                Hi again!

                I've done some further testing and I believe that there is something happening between PHP an Flash. When accessing the PHP script in a web browser the echo command gives (just the 'å,ä):


                However, when this is sent to Flash, the trace of the initial variable (before unescape()) is:


                I'm very confused.

                • 5. Re: PHP and Actionscript 3.0 issue
                  SymTsb Level 2
                  It seems to me like you are going through a few unnecessary hoops in your AS. I've also changed your data format to VARIABLES. If you're sending a URLVariables object to your PHP file through the POST method, then (at least in my opinion), you should be sending a variables based data format. I'm not sure that will have an ultimate impact but give it a shot. Flash can be quite particular about these little things. I eliminated the try/catch in favor of adding a second listener to handle the IO error you were trying to catch. This is a much simpler method in my opinion and it has the same effect when handled correctly. I hope this helps...

                  • 6. Re: PHP and Actionscript 3.0 issue
                    latrixie Level 1
                    Hi, and thanks for your reply.

                    Actually, I think I got it to work in a pretty strange way...

                    There was something dodgy happening in the PHP script, something indicated by others in different forums (see some of the comments further down on: http://se.php.net/urlencode). I read somewhere that PHP had an issue with urlencoding and one solution was to urlencode twice. Having done that, things started to happen on the Flash side, but there was still some strange formatting going on. Eventually I solved it by changing my flash_encode() to:

                    PHP Code:

                    // --------------------------------------------------- ENCODE FOR FLASH
                    function flash_encode ($input) {
                    return rawurlencode(rawurlencode(($input)));

                    That is, using 'rawurlencode()' and no utf8_decode().

                    In my Flash file I then simply had to unescape twice:

                    AS Code:

                    function completeHandler(event:Event):void {
                    var loader2:URLLoader = URLLoader(event.target);
                    loader2.dataFormat = URLLoaderDataFormat.VARIABLES;
                    var variables2:URLVariables = new URLVariables(loader2.data);
                    var temp:String = String(variables2);
                    trace(temp); // just to check the "raw" data
                    var temp2 = unescape(temp);
                    var temp3 = unescape(temp2);

                    The output is now as expected!

                    Please note that I am not very skilled in programming so if anyone has a better solution please submit it. I'll use this for now though