【问题标题】:How do I integrate Flash with Php如何将 Flash 与 PHP 集成
【发布时间】:2011-05-23 05:46:25
【问题描述】:

我想将我的 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 脚本的新手,请指导我如何解决这些问题

【问题讨论】:

  • LoadVars 是一个 AS2 类 - 我将向您展示一个 AS3 示例。

标签: php mysql flash


【解决方案1】:

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

  1. 网址请求
  2. URLLoader
  3. URL 变量

这是我为你准备的快速课程:

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

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


我会把这个扔进去,因为你的问题有 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"];
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2011-08-03
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-01
    • 2014-02-13
    相关资源
    最近更新 更多