开发者

How do I integrate Flash with Php

I want to integrate my flash file with php code. I got the below code from

http://www.kirupa.com/developer/actionscript/flashphpxml_integration2.htm

function lv(l, n, t, e, f) {
    if (l == undefined) {
        l = new LoadVars();
        l.onLoad = function () {
            var i;
            n.htmlText = "";
            if (t == undefined) {
                n.htmlText += "<b>" + this["title" + e] + "</b><br>";
            } else {
                for (i = 0; i < this.n; i++) {
                    n.htmlText += "<a href='" + this["link" + i] + "'>" + this["title" + i] + "</a><br>";
                }
            }
        };
    }
    l.load(f);
}
lv(sites_txt, "cycle", null, "sites.php");

I did all the steps given in that forum but while running that code i got error Like

1180: Call to a possibly undefined method LoadVars.

Warning: 1060: Migration issue: The method LoadVars is no longer supported.  For more information, see the URLVariables class, the URLRequest.urlVariables and URLRequest.postData properties, and the URLLoader.dataFormat property..

1136: Incorrect number of arguments.  Expected 5.

I am new to the flash scrip开发者_开发知识库ting please guide me how to troublshooting these


Your example code was in AS2, here's how you send and receive data from and to PHP using AS3 using:

  1. URLRequest
  2. URLLoader
  3. URLVariables

Here's a quick class I have made for you:

package
{
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.events.Event;

    public class PHPData extends Object
    {
        /**
         * Sends data to a PHP script
         * @param script A URL to the PHP script
         */
        public function send(script:String, vars:URLVariables):void
        {
            var req:URLRequest = new URLRequest(script);

            req.data = vars;
            req.method = URLRequestMethod.POST;

            var loader:URLLoader = new URLLoader();
            loader.load(req);

            // listeners
            loader.addEventListener(Event.COMPLETE, _complete);
        }

        /**
         * Called when a response has been received from a PHP script
         * @param e Event.COMPLETE
         */
        private function _complete(e:Event):void
        {
            var vars:URLVariables = new URLVariables(e.target.data);

            var i:String;
            for(i in vars)
            {
                trace(i + ": " + vars[i]);
            }

            e.target.removeEventListener(Event.COMPLETE, _complete);
        }
    }
}

With this, you can send data to a given PHP script in the format of URLVariables.

URLVariables are easily prepared like so:

var vars:URLVariables = new URLVariables();

vars.myvar = "some value";
vars.myothervar = 30;

Here's a quick example I have mocked up for you which sends a string to PHP, then PHP sends back the string hashed as MD5 and also has a timestamp attached as a secondary value.

var php:PHPData = new PHPData();

var vars:URLVariables = new URLVariables();
vars.myvar = "marty";

php.send("http://projectavian.com/md5.php", vars);

Your output for this will be something similar to:

response: bb3761a33402b4f82806178e79ec5261
time: 1306133172

Just alter the _complete method in the PHPData class to handle your response data as required :)


I'll throw this in because your question has the mysql tag..

All you need to do is your standard INSERT and SELECT queries in the PHP script and encode your result into this format:

var=1&other=2&more=three

So you could have..

<?php
    mysql_connect(/* ?? */);
    mysql_select_db(/* ?? */);

    // INSERT example
    $thing = mysql_real_escape_string($_POST["thing"]);
    mysql_query("INSERT INTO table VALUES('','$thing')");

    // SELECT for response
    $id = mysql_real_escape_string($_POST["uid"]);
    $query = mysql_query("SELECT * FROM table WHERE id='$uid' LIMIT 1");

    // send response
    $r = mysql_fetch_assoc($query);
    echo 'fname=' . $r["first_name"] . '&lname=' . $r["last_name"];
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜