AMFPHP配置详细教程 不想传递拆分字符串,解析串行XML了吧? 厌烦JSON了? 试试AMF吧,传送多种数据格式。 首先下载AMFPHP(本教程使用1.9BETA2)你可以选择其他版本  在这里选择下载 下载后,解压缩,把文件夹中的amfphp文件夹拷贝到 APACHE服务器的网站根目录。 然后打开浏览器,输入 http://localhost/amfphp/gateway.php    确定 如果提示: amfphp and this gateway are installed correctly. You may now connect to this gateway from Flash. Note: If you're reading an old tutorial, it will tell you that you should see a download window instead of this message. This confused people so this is the new behaviour starting from amfphp 1.2. View the amfphp documentationLoad the service browser 说明安装成功。 打开网站根目录的AMFPHP文件夹,进入SERVICES文件夹,新建一个PHP文件命名为HELLOWORLD.php 拷贝以下代码: <?php class HelloWorld { function HelloWorld() { $this->methodTable = array ( "say" => array ( "access" => "remote", "description" => "Pings back a message" ) ); } function say($sMessage) { return 'You said: ' . $sMessage; } } ?> 保存! 在浏览器地址栏输入 http://localhost/amfphp/browser/ 回车 AMFPHP的项目管理器: 单击 HelloWorld 看到一下内容,就是HELLOWORLD项目具体内容:sMessage输入内容(比如"xiaoxiaocainiao")  单击call按钮: 看到页面下方的提示结果: 好了,你已经初步学会安装AMFPHP并使用了,具体的语法,函数,还需要查看一下官方的文档啦。 下面一起来做一个FLASH通过AMFPHPPHP交互的小例子: 到这里下载AMFPHP for FLASH8的组件,如果你使用其他版本,请到这里选择下载:http://www.adobe.com/products/flashremoting/downloads/components/ 下载后安装,完毕后打开FLASH ,查看 窗口|公用库|Remoting”就可以看到AMFPHP的组件了。 新建立一个工程,拖一个RemotingClasses组件到舞台的任何地方,再拖一个List组件,命名为myls,拖一个Button组件,命名为"mybt" 在第一帧的代码行复制以下代码: import mx.remoting.*; import mx.rpc.*; import mx.utils.Delegate; import mx.remoting.debug.NetDebug; var gatewayUrl:String = "http://localhost/amfphp/gateway.php"; var service:Service; NetDebug.initialize(); service = new Service(gatewayUrl, null, "PersonService"); function getPerson() { trace("getPerson"); var pc:PendingCall = service.getPerson(); pc.responder = new RelayResponder(this, "handleGetPerson"); } function handleGetPerson(re:ResultEvent) { trace("handleGetPerson"); trace(re.result.getItemAt(0).name); _root.mytree.dataProvider = re.result; _root.mydg.dataProvider = re.result; } function onClick() { getPerson(); } mybt.addEventListener("click", Delegate.create(this, onClick)); 在AMFPHPSERVICES文件夹下新建一个文件,命名为PersonService.php 复制一下内容到PersonService.php <?php class PersonService { function PersonService() { $this->methodTable=array( "getPerson"=> array( "access"=>"remote" ) ); mysql_connect('localhost','root','wuliqunao'); mysql_select_db('test'); } function getPerson() { $sql=sprintf("SELECT * FROM persons"); $query=mysql_query($sql); return $query; } } ?> 好了,发布你的程序,单击Button按钮,看到结果: 当然了,你的本机需要有个MYSQL数据有,里面简历一个test数据库,这个库里面创建一个persons数据表,表里2个字段,一个是name,一个是age.(表告诉我,你搞不好这个库啊,搜一个MYSQL数据库入门教程,一下就知道啦。) 好了,大功告成,开始学习AMFPHP之路吧~~~~ AMFPhp 与 Flex Builder3 的交互(一) 写一下php与Flex的交互。作为学习笔记,现在这方面中文文档貌似比较少。下面的大多数都来自http://www.sephiroth.it 。英文好的同学可以直接去看。 好,先说一下我的基本配置吧。php 版本:php-5.2.6-Win32.zip,apache版本:apache_2.2.8-win32-x86,mysql版本:mysql-5.0.51a-win32.具体的配置方法就不说了,去网上搜吧,太多了。我的本地服务放在 D:\php。 好,ok。 在http://sourceforge.net/project/showfiles.php?group_id=72483#files下载amfphp压缩包,我的版本是 amfphp-1.9.beta.20070513.zip 。下载后,解压到我的本地服务目录,即 D:\php 。然后再浏览器中输入:http://localhost/amfphp/gateway.php 如果你看到了如下信息,就说明安装成功了。 amfphp and this gateway are installed correctly. You may now connect to this gateway from Flash. AMF C Extension is loaded and enabled. Note: If you're reading an old tutorial, it will tell you that you should see a download window instead of this message. This confused people so this is the new behaviour starting from amfphp 1.2. View the amfphp documentation Load the service browser 好了,让我们写一个HelloWorld程序吧。首先打开D:\php\amfphp\services目录,新建一个HelloWorld.php文件,在文件中输入以下代码: 1. <?php 2. class HelloWorld 3. { 4. 5. function sayHello() 6. { 7. return "Hello World!"; 8. } 9.   } 10. ?> 复制代码 好啦,php端,就先这样了。然后我们写flex端的吧。在Flex builder3新建一个Flex工程。 需要注意的是,在Application server type选择php。呵呵,下一步。 在web root 下填入 D:\php 即我的本地web服务的目录。root url 填入:http://127.0.0.1 即本地IP啦,呵呵。然后下一步,如果你不改源文件的目录的话,就直接Finish啦。 好,在src里再新建一个xml文档,名字叫: services-config.xml ,打开,输入以下代码,这个flex与amfphp的配置文件。 1. <?xml version="1.0" encoding="UTF-8"?> 2. <services-config> 3. <services> 4. <service > 5. <destination > 6. <channels> 7. <channel ref="my-amfphp"/> 8. </channels> 9. <properties> 10. <source>*</source> 11. </properties> 12. </destination> 13. </service> 14. </services> 15. <channels> 16. <channel-definition > 17. <endpoint uri="http://127.0.0.1/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/> 18. </channel-definition> 19. </channels> 20. </services-config> 复制代码 然后右键点击你的项目,选择Properties选项,在弹出的对话框里选择Flex Compiler,在Additional compiler arguments里加入 -services "services-config.xml",如图: 点击src ,打开AMFphp.mxml,好输入以下代码: 1. <?xml version="1.0" encoding="utf-8"?> 2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html"> 3.     <mx:RemoteObject > 4.         <mx:method name="sayHello" result="resultHandler(event)" /> 5.     </mx:RemoteObject> 6. 7.     <mx:Script> 8.         <![CDATA[ 9.             import mx.managers.CursorManager; 10.             import mx.rpc.events.ResultEvent; 11.             import mx.rpc.events.FaultEvent; 12.             private function faultHandler(fault:FaultEvent):void 13.             { 14.                 CursorManager.removeBusyCursor(); 15.                 result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail; 16.             } 17. 18.             private function resultHandler(evt:ResultEvent):void 19.             { 20.                 result_text.text = evt.message.body.toString(); // same as: evt.result.toString(); 21.             } 22.         ]]> 23.     </mx:Script> 24. 25.     <mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/> 26.     <mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('foo').send(); "/> 27.     <mx:TextArea x="10" y="36" width="319" height="113" /> 28.     <mx:Label x="10" y="10" text="Result:"/> 29. </mx:Application> 复制代码 好,先不管代码的含义,我们运行一下吧,如果你看见了下面的图片,你就成功了。 点击sayHello按钮,看见它和你问好了吗? 看见了!oh,Good.你太有天赋了,不编程可惜了,呵呵。今天先到这,有空继续。 (本文与作者博客同步更新,呵呵,做个广告,博客地址在下面) 好,让我们来分析一下这段代码吧! 在<mx:RemoteObject>下呢,定义的就是RemoteObject相关的一些东东.(这话题听着别扭),id这个就不解释了,fault就是当服务调用失败并且操作自身不处理时,调度fault事件。showBusyCursor呢,如果为 true,在执行服务时就会显示忙碌光标。在这里重点是后两个属性,sourcedestinationsource后面写的是,你在服务器上的文件名,也就是咱们用php写的那个类的文件的名字,即在D:\php\amfphp\services目录下,这个算是咱们本地服务的根目录。如果你要建一个新的目录为hello,然后把HelloWorld.php这个文件放进去,那么我们的程序应该改一下,即source = "hello.HelloWorld" destination有代表什么呢?服务的目标。这个值应该与services-config.xml文件中的目标条目匹配,那好,去找一下services-config.xml这个文件吧,看看有这个叫做destination这个东东吗?该 <mx:method />这行了,很简单,这个就是咱们要用的方法啊,名字和php里面方法的名字要一致哦。 另外,需要注意的是在我们的显示界面的代码中,在labelsayHello<mx:Button />里面,click = "myservice.getOperation('sayHello').send" ,这行代码的意思就是当我们点击按钮的时候,将要执行我们的remoteObject对像中的sayHello方法,并且等待result事件,如果一切顺利,php端会给我们返回一个叫做"Hello world"的字符串,然后,将去调用<![CDATA  .....  ]]>里面的ResultHandler函数,接着我们的result_text文本就有任务啦,那就是显示这个字符串。怎么样,很简单吧。(其他的请看源代码注释,我也是刚研究,有错误的地方请指正)哇,今天学到了不少东西,明天继续。原来世界这么美好!  1. <?xml version="1.0" encoding="utf-8"?> 2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html"> 3.     <mx:RemoteObject > 4.         <mx:method name="sayHello" result="ResultHandler(event)" /> 5.     </mx:RemoteObject> 6. 7.     <mx:Script> 8.         <![CDATA[ 9.             import mx.managers.CursorManager; 10.             import mx.rpc.events.ResultEvent; 11.             import mx.rpc.events.FaultEvent; 12.             //注:错误处理函数 13.             private function FaultHandler(fault:FaultEvent):void 14.             { 15.                                 //注:删除忙状态光标               16.                 CursorManager.removeBusyCursor(); 17.                 //注:返回的错误信息,让其在result_test中显示出来 18.                 result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail; 19.             } 20. 21.             private function ResultHandler(evt:ResultEvent):void 22.             { 23.                 //注: 将获得的结果转化为String类型,并交给result_text显示出来 24.                 result_text.text = evt.message.body.toString(); // same as: evt.result.toString(); 25.             } 26.         ]]> 27.     </mx:Script> 28.         <!--下面这个东东呢,就是我们的显示界面了,2个按钮、一个文本区域、一个标签。 --> 29.     <mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/> 30.     <mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('hoo').send(); "/> 31.     <mx:TextArea x="10" y="36" width="319" height="113" /> 32.     <mx:Label x="10" y="10" text="Result:"/> 33. </mx:Application> 复制代码 AmfphpFlex交互--(三 ) 向php端写数据,并读返回数据 好,还记得我们第一章讲得吗?不记得啦?看下这个链接啊 http://bbs.actionscript3.cn/thread-16541-1-1.html 我们今天做的呢,前端,只要在第一章代码的基础上修改一下就好了,主要的是php端的改变。 所以呢,你可以在你以前的工程里面新建一个Application,然后粘贴下面的代码哦。 先看一下flex端的代码: 1. <?xml version="1.0" encoding="utf-8"?> 2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF" viewSourceURL="srcview/index.html"> 3.     <mx:RemoteObject > 4.         <mx:method name="InputUser" result="ResultHandler(event)" /> 5.     </mx:RemoteObject> 6. 7.     <mx:Script> 8.         <![CDATA[ 9.             import mx.managers.CursorManager; 10.             import mx.rpc.events.ResultEvent; 11.             import mx.rpc.events.FaultEvent; 12.             //注:错误处理函数 13.             private function FaultHandler(fault:FaultEvent):void 14.             { 15.                                 //注:删除忙状态光标               16.                 CursorManager.removeBusyCursor(); 17.                 //注:返回的错误信息,让其在result_test中显示出来 18.                 result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail; 19.             } 20. 21.             private function ResultHandler(evt:ResultEvent):void 22.             { 23.                 //注: 将获得的结果转化为String类型,并交给result_text显示出来 24.                result_text.text = evt.message.body.toString(); // same as: evt.result.toString(); 25.             } 26.         ]]> 27.     </mx:Script> 28.         <!--下面这些东东呢,就是我们的显示界面了,2个按钮、一个文本区域、一个标签。 --> 29.     <mx:Button x="250" y="157" label="Send" width="79" click="myservice.getOperation('InputUser').send(result_text.text);"/> 30.     <mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('hoo').send(); "/> 31.     <mx:TextArea x="10" y="36" width="319" height="113" /> 32.     <mx:Label x="10" y="10" text="Result:"/> 33. 34. </mx:Application> 复制代码 发现变化了吗?没有?再仔细瞅瞅,哦!原来,在第三行,source里面,多了一个这个东东"hello.AddEmialDb",这个是干什么的呢?就是帮助flex找到php文件啊。在第4行,即 定义RemoteObject的里面,我们换了一个叫做InputUser的函数,当然,这个一定是php端函数啦。在第29行,按钮的click事件里面getOperation参数也同样改变为InputUser,另外send()函数里面好像多了一个文本信息,猜猜?恩,对,这个就是要向Amfphp端发送的东东。很好理解。对吧? 然后是php端,在D:\webroot\amfphp\services\ 新建一个hello目录,在这个目录中再新建一个叫做AddEmailDb.php的文件。然后用你所熟悉的编辑器打开它,敲入以下代码:(傻瓜,我才不敲呢,我要copy过去。” 好吧,随你!)  1. <?php 2. class AddEmailDb 3. { 4.         function InputUser($post) 5.           { 6.                 global $host,$dbTable, $dbPass,$dbId; 7.                 $host = "localhost"; 8.                 $dbId = "root"; 9.                 $dbPass = "123456"; 10.                 $dbTable = "mmmm"; 11. 12.                 $link = mysql_connect($host,$dbId,$dbPass); 13.                 mysql_select_db($dbTable); 14.                 if(!$link) 15.                 { 16.                         die('Connection Impossible:' . mysql_error()); 17.                 } 18.                 else 19.                 { 20.                         $sql = "Insert INTO mmmm(id, adressemail) VALUES ('','$post')"; 21.                         mysql_query($sql); 22. 23.                 } 24.                 mysql_close($link); 25.                 return "success"; 26. 27.         } 28. } 29. ?> 复制代码 运行你的Apache ,开启你的mysql。在mysql里面要建立一个数据库(我用的是phpMyAdmin

相关文章:

  • 2021-03-30
  • 2022-01-09
  • 2022-02-26
  • 2021-12-23
猜你喜欢
  • 2021-08-11
  • 2021-11-22
  • 2021-04-08
  • 2021-12-04
  • 2021-11-21
  • 2021-04-20
相关资源
相似解决方案