<?php
// Create new service for PHP Remoting as Class
class sample
{
function sample ()
{
// Define the methodTable for this class in the constructor
$this->methodTable = array(
"getUsers" => array(
"description" => "Return a list of users",
"access" => "remote"
)
);
}
function getUsers () {
$mysql = mysql_connect(localhost, "username", "password");
mysql_select_db( "sample" );
//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
while ($row = mysql_fetch_object($Result)) {
$ArrayOfUsers[] = $row;
}
return( $ArrayOfUsers );
}
}
?>
<!--当FLASH应用载入时调用initApplication ,设置gateway变量为到AMFPHP中的gateway.php的连接路径。这样就可以调用sample类中的getUsers方法了。然后根据是否出错相应地执行onResulth或者onFault两个函数之一。-->
public function initApplication()
{
gateway = new RemotingConnection( "http://localhost/flex/php/gateway.php" );
gateway.call( "sample.getUsers", new Responder(onResult, onFault));
}
<!--简单设置了dataProvider变量为结果变量,其通过AMFPHP传回ActionScript。这就是当你运行MYSQL查询($ArrayOfUsers)时会返回PHP对象的数组。AMFPHP已经自动把PHP对象数组翻译成了ActionScript数组,酷吧。 -->
public function onResult( result : Array ) : void
{
dataProvider = result;
}
<!--在出错情况下,就象给用户的错误信息提示一样,你可以追踪变量的值,在调试模式中特别有用-->
public function onFault( fault : String ) : void
{
trace( fault );
}
]]>
</mx:Script>
</mx:Application>
上述实例稍显复杂,让我们再次明确一下文件的位置。有三个文件被创建,以及下载来的AMFPHP文件包。