Search code examples
flashpostofflineurlloaderurlrequest

Post values to a URL using Flash


I'm dipping my toes into Flash development and wondered how to post some variables to a URL. Let's say the user has played a Flash game, packaged as an EXE or SWF embedded in HTML stored on the user's computer, not from some webpage, and would like to register the score by completing a simple form with just an e-mail address and pressing a button.

Would it be possible to do it even thought the Flash application is not on an active webpage?


Solution

  • It's possible but you need some server side scripts as well like PHP. Check out http://www.gotoandlearn.com for some awesome tutorials.

    Basically you create a URLRequest to a server side script and send some data with it. You can use URLVariables to pass data to the script. The script can then receive the data and save it in a database or send a mail.

    This is from the Adobe docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLVariables.html

    public function URLVariablesExample() {
                var url:String = "http://www.example.com/script.php";
                var request:URLRequest = new URLRequest(url);
                var variables:URLVariables = new URLVariables();
                variables.exampleSessionId = new Date().getTime();
                variables.exampleUserLabel = "guest";
                request.data = variables;
                navigateToURL(request);
            }
    

    On the PHP side you could do something like this:

    $exampleSessionId = $_REQUEST['exampleSessionId'];
    $exampleUserLabel = $_REQUEST['exampleUserLabel'];
    $message = "Id: " . $exampleSessionId . ", Label: " . $exampleUserLabel;
    mail('toaddress@example.com', 'My Subject', $message);