Flash使用AMF与PHP通信

作者: 2010-08-12,02:25 下午 周四

1, 下载 amfphp 及其示例代码 : http://www.amfphp.org/
2, amf核心放web目录,示例代码中的 services 放核心对应目录下
3, 修改 Main.as 的 gateway:String = “http://127.0.0.1/amfphp/gateway.php” 指向 web 目录
4, 修正: 核心中默认打开了session,不要就删掉session_start()
如果数据库是UTF-8编码,gateway.php中的编码设定: $gateway->setCharsetHandler(“none”, “ISO-8859-1″, “ISO-8859-1″);
作为加速,可加载AMFEXT扩展,使服务器支持AMF格式的编/解码:

http://www.teslacore.it/wiki/index.php?title=AMFEXT


Linux下的安装:
sh# tar -zxvf amfext*.tgz
sh# cd amfext*
sh# phpize
sh# ./configure
sh# make
sh# make install
将提示在相应目录下生成了: amf.so 文件
拷贝至扩展目录,并在php.ini文件下加入:
extension=amf.so
重启apache

gateway.php设置:
$gateway->setCharsetHandler(“none”, “ISO-8859-1″, “ISO-8859-1″); //数据库UTF-8编码时调这个,注释后好像就OK了
$gateway->logIncomingMessages(‘./in/’); //调试用.flash传递过来的参数都会保存到此目录下,通过$HTTP_RAW_POST_DATA或php://input获取
$gateway->logOutgoingMessages(‘./out/’); //调试用.返回给flash的数据都会保存到此目录下
$gateway->disableNativeExtension(); //禁用服务器amf扩展,如果加载了amfext扩展,由于此扩展有缺陷,就应该调用这个(如数字数组下标必须从0开始,否则出错)
$gateway->enableGzipCompression(25*1024); //如果设置,则如果数据量大于此数将采取zlib压缩,需要zlib支持

flash传给php的参数没有经过任何处理,注意要 addslashes(),intval()等处理

amf3比amf0更高效,如果是flash9及以上的版本,可以用amf3:
as3中:

1
2
3
4
5
import flash.net.ObjectEncoding;
...
this.objConnect = new NetConnection;
this.objConnect.objectEncoding = ObjectEncoding.AMF3;
//调用写好的程序都要通过gateway.php去调用

php中:
修改 shared/app/Globals.php 中的 $amfphp['encoding'] = “amf0″; 为 $amfphp['encoding'] = “amf3″;

AMF协议通讯抓包工具:
关键字: amf sniffer capture
charlesproxy http://www.xk72.com/charles/ //试用30天,经测试可以看到AMF数据
ieinspector http://www.ieinspector.com/index.html //可以看到AMF数据
WebScarab http://www.owasp.org/index.php/Main_Page
serviceCapture http://www.kevinlangdon.com/serviceCapture/ //14天试用
httpdebugger http://www.httpdebugger.com/

Flash播放器Debugger版:

http://www.adobe.com/support/flashplayer/downloads.html

AmfPHP的Bug:
1,flash端传负数,会得一个4亿多的值,http://www.visible-form.com/blog/sending-negative-integers-through-amfphp/解决:
amfphp/amf/io/AMFDeserializer.php 文件中的readAmf3Int函数替换成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function readAmf3Int(){
    $res = 0;
    $int = $this->readByte();
    if($int <128) {
        return $int;
    } else {
        $int = ($int & 0x7f) <<7;
        $tmp = $this->readByte();
        if($tmp <128) {
            $int |= $tmp;
        }else{
            $int = ($int | ($tmp & 0x7f)) <<7;
            $tmp = $this->readByte();
            if ($tmp < 128) {
                $int |= $tmp;
            }else {
                $int = ($int | ($tmp & 0x7f)) << 8;
                $tmp = $this->readByte();
                $int |= $tmp;
            }
        }
    }
    $mask = 1<<28;
    $res = -($int & $mask) | $int;
    return $res;
}

2,amfphp/shared/adapters/zendrowsetAdapter.php 最后两行有语法错误

刚安装好后的错误,处理方法:
Error #2044: 未处理的 NetStatusEvent:。 level=error, code=NetConnection.Call.BadVersion。
打开amf/gateway.php,找到代码:

1
2
3
4
5
6
7
    if(PRODUCTION_SERVER)
    {
        //Disable profiling, remote tracing, and service browser
        //$gateway->disableDebug();//注释
        // Keep the Flash/Flex IDE player from connecting to the gateway. Used for security to stop remote connections.
        //$gateway->disableStandalonePlayer();//注释
    }

暂无相关文章

分类 : FLASH,PHP (阅览:) Tags :

评论
2011年1月4日

|:victory:| Error #2044: 未处理的 NetStatusEvent:。 level=error, code=NetConnection.Call.BadVersion。

找了很久!!! 感謝大大

Posted by 匿名
2011年1月24日

AMF配置有问题,在PHP里找

Posted by 嘎嘎
留下评论


|:victory:| |:tongue:| |:titter:| |:time:| |:sweat:| |:smile:| |:shy:| |:shocked:| |:sad:| |:mad:| |:lol:| |:kiss:| |:hug:| |:huffy:| |:handshake:| |:cry:| |:call:| |:biggrin:|