PS:原创文章,如需转载,请注明出处,谢谢!
本文地址:http://flyer0126.iteye.com/blog/2188849
AMFPHP是PHP的远程调用(RPC, Remote Procedure Call)工具。由于近期提供接口给Flash,考虑使用AMFPHP开发,简单整理一下。
1. 下载 http://www.silexlabs.org/amfphp/
2. 目录结构
amfphp-2.2.1/Amfphp -- 核心程序库
amfphp-2.2.1/BackOffice -- 测试控制面板
amfphp-2.2.1/Examples -- 客户端(Flash、Flex、Javascript)请求示例及服务端(Php)程序示例
amfphp-2.2.1/goodies -- 图片文件夹
3. 编写测试服务
新建HelloworldService.php 文件,内容如下:
<?php
/**
* Created by PhpStorm.
* User: flyer0126
* Date: 15-3-4
* Time: 下午6:20
*/
class HelloworldService{
public function sayHelloWorld($value){
return "hello world ".$value;
}
}
4. 测试服务
利用BackOffice中ServiceBrowser,设置BackOffice登陆用户名/密码(Config.php文件):
//example code for username + password:
$this->backOfficeCredentials['admin'] = '123456';
也可设置$requireSignIn为false,取消用户验证。
登陆成功后,ServiceBrowser内容可见。
5. 客户端调用
参考Examples/Javascript/下示例,请求服务端服务。
function call() { /** * notes: * - parameters here could be left empty, they are just here to make the code easier to adapt * - $.post method here is used because amfPHP JSON plugin expects data as POST. So can't use more obvious getJSON * - if you always use the same parameters, you can do without json2.js, by setting for example * callData = '{"serviceName":"PizzaService", "methodName":"getPizza","parameters":[]}' */ var callData = JSON.stringify({"serviceName":"HelloWorld", "methodName":"sayHelloWorld","parameters":['11']}); $.post("http://localhost/amfphp/Amfphp/index.php?contentType=application/json", callData, onSuccess); } function onSuccess(data) { alert("result : " + data); }
触发执行函数call(),请求amfphp测试服务,alert测试信息“result:11”即算成功。