如何将Flash与Php集成

时间:2011-05-23 05:46:26

标签: php mysql flash

我想将我的flash文件与php代码集成。我从

获得了以下代码
  

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");

我完成了该论坛中给出的所有步骤,但在运行该代码时出现错误,如

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.

我是flash脚本的新手,请指导我如何解决这些问题

1 个答案:

答案 0 :(得分:4)

您的示例代码在AS2中,以下是使用AS3从PHP发送和接收数据的方式:

  1. 的URLRequest
  2. 的URLLoader
  3. 的URLVariables
  4. 这是我为你做的一个快速课程:

    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);
            }
        }
    }
    

    通过这种方式,您可以以URLVariables

    的格式将数据发送到给定的PHP脚本

    URLVariables很容易做好准备:

    var vars:URLVariables = new URLVariables();
    
    vars.myvar = "some value";
    vars.myothervar = 30;
    

    这是我为你模拟的一个快速示例,它向PHP发送一个字符串,然后PHP将字符串哈希值作为MD5发回,并且还附加了一个时间戳作为辅助值。

    var php:PHPData = new PHPData();
    
    var vars:URLVariables = new URLVariables();
    vars.myvar = "marty";
    
    php.send("http://projectavian.com/md5.php", vars);
    

    您的输出类似于:

    response: bb3761a33402b4f82806178e79ec5261
    time: 1306133172
    

    只需更改_complete类中的PHPData方法即可根据需要处理您的响应数据:)


    我会把它扔进去,因为你的问题有 mysql 标签..

    您需要做的就是在PHP脚本中使用标准的INSERT和SELECT查询,并将结果编码为以下格式:

    var=1&other=2&more=three
    

    所以你可以......

    <?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"];
    ?>