AS-AMFPHP AS和AMFPHP的数据交互
What's AMFPHP?官方的介绍如下:
AMFPHP is a free open-source PHP implementation of the Action Message Format(AMF). AMF allows for binary serialization of Action Script (AS2, AS3) native types and objects to be sent to server side services. AMFPHP is challenged with implementing the entire AMF protocol to be an alternative to Flex Data Services (AMF3) and Flash Remoting (AMF0). AMFPHP allows thin client applications built in languages such as Flash, Flex, and AIR to communicate directly with PHP class objects on the server. PHP developers can leverage their PHP experience in server side code development by connecting to data sources such as web-services, databases, and business applications and return that data to the client. Client applications can also offload cpu intensive methods to PHP services and wait for the result set for presentation to the user.......
官方的下载包虽然给了几个Demo,但是都没有操作数据库,刚好学习AMFPHP,做了个Demo,共享下,Demo如下:
下边是代码部分,先来AS的:
//flash跟amfphp通信的简单例子
package {
import flash.display.MovieClip;
import fl.events.*;
import flash.events.*;
import flash.text.TextField;
import flash.net.NetConnection;
import flash.net.Responder;
public class Main extends MovieClip {
private var gateway:String = "http://192.168.1.166/flashtest/as2php/amfphp/gateway.php";//amfphp gateway.php文件路径
private var connection:NetConnection;
private var responderSet:Responder;
private var responderGet:Responder;
public function Main() {
//注册鼠标事件
btnSet.addEventListener(MouseEvent.CLICK, setData);
btnGet.addEventListener(MouseEvent.CLICK, getData);
//设置responder
responderSet = new Responder(onResultSet, onFault);
responderGet = new Responder(onResultGet, onFault);
connection = new NetConnection;
//链接gateway
connection.connect(gateway);
}
//发送数据给amfphp
public function setData(_evt:MouseEvent):void {
//获取输入框内的数据
var info1 = txtInput1.text;
var info2 = txtInput2.text;
var info3 = txtInput3.text;
//调用amfphp的函数
connection.call("EasyAMF.setData", responderSet, info1,info2,info3);
txtResponse.text = "Sending Data to AMFPHP";
}
//接收来自amfphp的数据
public function getData(_evt:MouseEvent):void {
connection.call("EasyAMF.getData",responderGet);
txtResponse.text = "Geting data from AMFPHP";
}
//成功调用amfphp函数后的处理函数
private function onResultSet(result:Object):void {
txtResponse.text = String(result);
}
private function onResultGet(result:Object):void {
var aryRs:Array=new Array(); //用一个数组来接收返回的记录
aryRs=result["serverInfo"]["initialData"]; //从数据库中传来的值为何是2维数据其实我也没去看,google来的资料上说这么写~。~
rsinput1.text=aryRs[0][1]; //显示数据
rsinput2.text=aryRs[0][2];
rsinput3.text=aryRs[0][3];
}
//调用amfphp函数失败后的处理函数
private function onFault(fault:Object):void {
txtResponse.text = String(fault.description);
}
}
}
接着是amfphp/services/EasyAMF.php,这部分是简单的SQL语句,就不写注释了:
< ?php
class EasyAMF
{
public function EasyAMF(){
global $host,$dbTable, $dbPass,$dbId;
$host = "localhost";
$dbId = "root";
$dbPass = "";
$dbTable = "amfphp";
$dblink = mysql_connect($host,$dbId,$dbPass);
mysql_select_db($dbTable);
mysql_query("SET NAMES UTF8");
if(!$dblink)
{
die('Connection Impossible:' . mysql_error());
}
}
public function setData($info1,$info2,$info3)
{
$sql="INSERT INTO info (id ,info1 ,info2 ,info3)VALUES (NULL , '$info1', '$info2', '$info3');";
$rs=mysql_query($sql);
if(!$rs==1){
return "failed";
}else{
return "success";
}
}
public function getData()
{
$sql="SELECT * FROM info ORDER BY id DESC LIMIT 1";
$rs=mysql_query($sql);
return $rs;
}
}
?>
原先在做留言板的时候是准备用Ajax来进行数据交互,不过有了AMFPHP后,就没这个想法了。
源文件地址:http://dl.getdropbox.com/u/477487/flash/EasyAMF.rar
March 8th, 2009 - 20:01
针对数据库乱码的情况,请修改gateway.php的$gateway->setCharsetHandler(“utf8_decode”,”ISO-8859-1″,”ISO-8859-1″);为$gateway->setCharsetHandler(“utf8_decode”,”utf-8″,”utf-8″);另外在读取数据的时候,请记得先执行mysql_query(“SET NAMES UTF8″);
[Reply]
June 23rd, 2009 - 09:36
我用您这个教程有错误提示啊,
ArgumentError: Error #2173: 无法读取流中的对象。类 flex.messaging.io.ArrayCollection 虽未实现 flash.utils.IExternalizable,但由其别名可得知它为 externalizable 类。
object
null
TypeError: Error #1009: 无法访问空对象引用的属性或方法。
at Main/onResultGet()
[Reply]
DFdou Reply:
2009-6-23 at 9:46 am
@jyhs, 恩,你用的是我的源文件么?啊……你是用的Flex?两边类包可能有所不同,类 flex.messaging.io.ArrayCollection 虽未实现 flash.utils.IExternalizable,但由其别名可得知它为 externalizable 类??这部分是?
[Reply]
jyhs Reply:
2009-6-23 at 11:13 am
对啊,我用的是你的源文件的,我忘记说了,我用的是FLASH CS4平台的,不是FLEX啊,难道您这个教程是用在FLEX中的,能放到FLASH平台上吗,我对FLEX不熟悉的,能帮忙在解一下吗,
June 23rd, 2009 - 11:44
哈哈,在线等啊
[Reply]
DFdou Reply:
2009-6-23 at 2:56 pm
@jyhs, 我发邮件给你了~我这个是Flash CS3的文件,跟CS4应该兼容的~我测试下看……
[Reply]
jyhs Reply:
2009-6-23 at 3:32 pm
@DFdou, 测试如何?我又给你回了邮件,里面说了一下具体的情况,请你看一下我的问题,谢谢!
DFdou Reply:
2009-6-23 at 3:09 pm
@jyhs, 刚测试过了CS4,没问题啊我这里……
[Reply]
DFdou Reply:
2009-6-23 at 3:10 pm
@jyhs, 你设置过AMFPHP的gateway了么 private var gateway:String = “http://192.168.1.166/flashtest/as2php/amfphp/gateway.php”;//amfphp gateway.php文件路径?
[Reply]
jyhs Reply:
2009-6-23 at 3:38 pm
@DFdou, 是的,基础上就是改了这个啊,这样吧,我把代码发到邮件中,发完了,
June 23rd, 2009 - 17:05
k5love,你真是个大好人,谢谢你,帮我研究了一下午的代码!!
[Reply]
DFdou Reply:
2009-6-23 at 8:40 pm
@jyhs,
[Reply]
October 25th, 2009 - 05:14
ReferenceError: Error #1069: 在 Boolean 上找不到属性 serverInfo,且没有默认值。
at Main/::onResultGet()
55555
楼主。我点了下btnGet按扭怎么会出现这个错误啊
[Reply]
DFdou Reply:
2009-10-26 at 6:54 pm
@aser, 应该是Flash Player版本的问题,我这边没问题
[Reply]
December 10th, 2009 - 15:46
你好,我把通过amfphp连接数据库的那段代码单独写一个类里 可是在Main.as里获取不到查询出来的数据 能不能帮我看看是什么原因?在线等
[Reply]
DFdou Reply:
2009-12-10 at 4:15 pm
@sara, 有用AMFPHP么?
[Reply]
DFdou Reply:
2009-12-10 at 4:20 pm
@DFdou, 不通过AMFPHP单独写一个类是不行的,,单独写的话可以参照这个http://nwhy.org/flash-wish-php.html,用php输出xml给flash,不过效率有点差
January 29th, 2010 - 10:33
先谢谢您提供的教程,想请问能否给出数据表内容?谢谢
[Reply]
DFdou Reply:
2010-1-31 at 1:23 pm
@土豆, INSERT INTO info (id ,info1 ,info2 ,info3)VALUES (NULL , ‘$info1′, ‘$info2′, ‘$info3′);
[Reply]
May 24th, 2010 - 14:46
[Reply]