How to get started with the Remote Call (RPC, Remote Procedure Call) tool in AMFPHP

Source: Internet
Author: User

It enables PHP to communicate seamlessly with the following technologies:
(1) Flash and Flex Remoting
(2) JavaScript JSON and Ajax JSON
(3) XML and XML-RPC
What is RPC
Remote Procedure Call (RPC) is a data exchange method between the client and the server. We can call the local object to set the callback for various parameter methods and accept the call results. We do not need to care about the Implementation Details of sending and receiving data. Implementation Details are usually abstract, just as we call a local method.
How AMFPHP works
The client (Flash/Flex) and server (PHP) use the same method to describe method calls and complex data. The client serializes the request and sends it to the gateway AMFPHP. Run AMFPHP again:
(1) deserialization request
(2) Find the corresponding remote service class
(3) instantiation class
(4) perform security check
(5) (using the specified parameter) Call the server Method
(6) serialize the returned data
AMFPHP can correctly serialize and deserialize complex data types. In addition to objects and arrays, it also supports resources data to connect to resources, which means that we can simply return mysql_query by calling a remote method, and amfphp will handle all this. If the platform supports (currently, Flash Remoting and Flex Remoting), AMFPHP can also process cyclic references and custom data. It also supports simple remote debugging. AMFPHP also comes with a browser that can test remote services before creating client code. AMFPHP 1.0.1 also adds a template to automatically generate client code. AMFPHP 1.9 beta has added support for AMF3.
Simple Example
Next we will give a preliminary understanding of AMFPHP through a simple logon example, which will be introduced from the client and server respectively.
I. Flex client:
Code Copy codeThe Code is as follows: 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 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, submit data to the server
Var name: String = this.txt Name. text;
Var pwd: String = this.txt Password. 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 Failed:" + result [1]);
} Else if (flag = "1 "){
Alert. show ("Login successful:" + result [1]);
} Else if (flag = "-1 "){
Alert. show ("exception:" + result [1]);
}
}
Public function faultHandler (event: FaultEvent): void
{// Error handling
Alert. show ("sorry, Error !!! ");
}
}

2. PHP server
1. Place the amfphp folder in the root directory of the MyTest project. Open your browser and enter the following address to verify that amfphp is successfully installed.Copy codeThe Code is as follows: http: // localhost/MyTest/amfphp/gateway. php

Amfphp uses this gateway to locate our service classes and forward requests to these services for processing.
2. the Login. php file contains the Login class for processing Login requests. This file is placed in the BusinessLogic directory.
CodeCopy codeThe Code is as follows: <? 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 in globals. php as follows to specify the directory of the service class for amfphp.Copy codeThe Code is as follows: $ servicesPath = "../BusinessLogic /";

Author: Dongting sangren
AMFPHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.