AMFPHP PHP Remote Call of RPC Remote Procedure Call tool quick start tutorial

  • 2020-03-31 20:47:26
  • OfStack

It enables PHP to communicate seamlessly with the following technologies:
(1) Flash and Flex Remoting
(2) JavaScript JSON and Ajax JSON
(3) the XML and xml-rpc
What is the RPC
Remote Procedure Call is a way to exchange data between client and server. We can call the local object with a callback set to the various parameter methods and accept the result of the call. We don't have to worry about the implementation details of sending and receiving data. The implementation details are usually abstract, as if we were calling a local method.
How AMFPHP works
The client side (Flash/Flex) describes method calls and complex data in the same way as the server side (PHP). The client serializes the request and sends it to the gateway AMFPHP. AMFPHP then executes:
(1) deserialize the request
(2) find the corresponding remote service class
(3) instantiate the class
(4) perform safety checks
(5) invoke the server-side method (with the specified parameters)
(6) serialize the returned data
AMFPHP can serialize and deserialize complex types of data correctly. In addition to objects and arrays, it also supports resources data connection to resources, which means that we can simply return mysql_query by calling remote methods, which amfphp handles. If the platform supports (currently, Flash Remoting and Flex Remoting), AMFPHP can also handle circular references and custom data. It also supports simple remote debugging. And AMFPHP comes with a browser that can test remote services before creating client code. AMFPHP 1.0.1 also adds templates that automatically generate client code. The AMFPHP 1.9 beta added support for AMF3.
A simple example
Let's take a look at AMFPHP through a simple login example, from the client side and the server side.
I. Flex client:
code
 
import mx.controls.Alert; 
import mx.rpc.remoting.mxml.RemoteObject; 
import mx.rpc.events.*; 
public var login_remoteObj:RemoteObject = null; 
public function initLoginRemoteObject():void 
{//Initialize the RemoteObject
this.login_remoteObj = new RemoteObject(); 
this.login_remoteObj.source = "Login"; 
this.login_remoteObj.destination = "amfphp"; 
this.login_remoteObj.showBusyCursor = true; 
this.login_remoteObj.endpoint = "http://localhost/MyTest/amfphp/gateway.php"; 
this.login_remoteObj.doLogin.addEventListener("result", loginHandler); 
this.login_remoteObj.doLogin.addEventListener("fault", faultHandler); 
} 
public function doLogin():void 
{//Login operation to submit data to the server
var name:String = this.txtName.text; 
var pwd:String = this.txtPassword.text; 
var data:Array = new Array(); 
data.push(name); 
data.push(pwd); 
this.login_remoteObj.getOperation("doLogin").send(data); 
} 
public function loginHandler(event: ResultEvent):void 
{//Process the results returned by the server
var result:Array = event.result as Array; 
var flag:String = result[0]; 
if (flag == "0") { 
Alert.show(" Logon failure : " + result[1]); 
} else if (flag == "1") { 
Alert.show(" Log in successfully : " + result[1]); 
} else if (flag == "-1") { 
Alert.show(" abnormal : " + result[1]); 
} 
} 
public function faultHandler(event: FaultEvent):void 
{//Error handling
Alert.show("sorry, Make a mistake !!!"); 
} 
} 

Second, PHP server side
1. Place the amfphp folder in the root directory of the MyTest project, open the browser and enter the following address to verify whether the installation of amfphp is successful
 
http://localhost/MyTest/amfphp/gateway.php 

This is the gateway through which amfphp locates our service classes and forwards requests to them for processing.
2. Login.php file, which contains the Login class to process the Login request, is placed in the BusinessLogic directory
code
 
<?php 
class Login 
{ 
public function doLogin($data) 
{ 
$result = array(); 
try { 
$name = array_shift($data); 
$pwd = array_shift($data); 
if ($name == "phinecos" && $pwd == "123") { 
$result[] = "1"; 
$result[] = "you are valid user!"; 
} else { 
$result[] = "0"; 
$result[] = "login failed"; 
} 
} catch (Exception $ex) { 
$result[] = "-1"; 
$result[] = $ex->getMessage(); 
} 
return $result; 
} 
} 
?> 

3. Modify the service path entry in globals.php as follows to indicate the directory where the service class is located for amfphp
 
$servicesPath = "../BusinessLogic/"; 

Author: dongting scattered people
(link: #)

Related articles: