amfphp PHP remote Invoke (RPC, remote Procedure Call) tool Quick Start Tutorial _php instance

Source: Internet
Author: User
Tags error handling php server
It enables PHP to communicate seamlessly with the following technologies:
(1) Flash and Flex Remoting
(2) JavaScript JSON and Ajax JSON
(3) XML and RPC
What is RPC
Remote Program invocation (RPC, remote Procedure call) is a way for clients to exchange data with the server side. We can call the local object band to set the callback for the various parameter methods and accept the call result. We don't care about the implementation details of sending and receiving data. The implementation details are usually abstract, just as we are invoking the local method.
The working principle of amfphp
The client (Flash/flex) uses the same approach as the server side (PHP) to describe method invocations and complex data. The client serializes the request and sends it to the Gateway amfphp. amfphp re-execution:
(1) Deserialization request
(2) Find the appropriate remote service class
(3) Instantiation class
(4) Perform security checks
(5) Calling the server-side method (using the specified parameters)
(6) Serialization of returned data
Amfphp can serialize and deserialize complex type data correctly. In addition to objects and arrays, it also supports resources for resource data connectivity, which means that we can simply return to mysql_query,amfphp by invoking the remote method to handle all this. If platform support (for now, 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 the remote service before creating the client code. Amfphp 1.0.1 also adds a template that automatically generates client code. Amfphp 1.9 Beta is a new addition to AMF3 support.
Simple example
Below we have a simple login example to the amfphp have a preliminary understanding, will be from the client and server side two parts of the introduction.
One, Flex client:
Code
Copy Code code 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
{//initialization of 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 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
{//processing results returned by server
var Result:array = Event.result as Array;
var flag:string = result[0];
if (flag = = "0") {
Alert.show ("Landing failure:" + result[1]);
else if (flag = "1") {
Alert.show ("Landing success:" + result[1]);
else if (flag = "-1") {
Alert.show ("Abnormal:" + result[1]);
}
}
Public Function Faulthandler (event:faultevent): void
{//Error handling
Alert.show ("Sorry, wrong!!!");
}
}

Two, PHP server side
1, place the amfphp folder under the root directory of the MyTest project, open the browser and enter the address below to verify that amfphp was successfully installed
Copy Code code as follows:

http://localhost/MyTest/amfphp/gateway.php

Amfphp is the gateway that locates our service classes and forwards the requests to these service classes for processing.
The 2,login.php file, which contains the login class for processing login requests, is placed under the Businesslogic directory
Code
Copy Code code 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 entry in globals.php as follows, indicating the directory where the service class resides for amfphp
Copy Code code as follows:

$servicesPath = ". /businesslogic/";

Author: Cavern Scattered People
amfphp Download Address

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.