AMFPHP php遠程調(diào)用(RPC, Remote Procedure Call)工具 快速入門教程
Jun 13, 2016 pm 12:16 PM
它可以使PHP與下述技術(shù)無縫通信:
(1) Flash 和 Flex Remoting
(2) JavaScript JSON 和 Ajax JSON
(3) XML 和XML-RPC
什么是RPC
遠端程序調(diào)用(RPC, Remote Procedure Call) 是一種客戶端與服務(wù)器端交換數(shù)據(jù)方式。我們可以調(diào)用本地對象帶對各種參數(shù)方法設(shè)置回調(diào)并接受調(diào)用結(jié)果。我們不用關(guān)心發(fā)送和接收數(shù)據(jù)的實現(xiàn)細節(jié)。實現(xiàn)細節(jié)通常是抽象的,就像我們在調(diào)用本地方法一樣。
AMFPHP的工作原理
客戶端(Flash / Flex)與服務(wù)器端(PHP) 使用相同的方式描述方法調(diào)用和復雜數(shù)據(jù)??蛻舳诵蛄谢埱蟛⑺l(fā)送到網(wǎng)關(guān)AMFPHP。AMFPHP再執(zhí)行:
(1) 反序列化請求
(2) 找到相應(yīng)的遠程服務(wù)類
(3) 實例化類
(4) 執(zhí)行安全檢查
(5)(使用指定參數(shù))調(diào)用服務(wù)器端方法
(6) 序列化返回的數(shù)據(jù)
AMFPHP可以正確地序列化、反序列化復雜類型數(shù)據(jù)。除了對象和數(shù)組,它還支持 resources 數(shù)據(jù)連接資源,這就意味著我們可以通過調(diào)用遠程方法簡單返回mysql_query,amfphp 會處理這一切。如果平臺支持 (目前來說,F(xiàn)lash Remoting 和Flex Remoting),AMFPHP還可以處理循環(huán)引用和自定義數(shù)據(jù)它也支持簡單的遠程調(diào)試。還有AMFPHP附帶一個瀏覽器,它可以在創(chuàng)建客戶端代碼前測試遠程服務(wù)。AMFPHP 1.0.1還添加了模板,可以自動生成客戶端代碼。AMFPHP 1.9 beta更是新增了對AMF3的支持。
簡單示例
下面我們通過一個簡單的登錄示例來對AMFPHP有一個初步的認識,將分別從客戶端和服務(wù)器端兩個部分進行介紹。
一,F(xiàn)lex客戶端:
代碼
復制代碼 代碼如下:
import mx.controls.Alert;
import mx.rpc.remoting.mxml.RemoteObject;
import mx.rpc.events.*;
public var login_remoteObj:RemoteObject = null;
public function initLoginRemoteObject():void
{//初始化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
{//登陸操作,向服務(wù)器提交數(shù)據(jù)
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
{//處理服務(wù)器返回的結(jié)果
var result:Array = event.result as Array;
var flag:String = result[0];
if (flag == "0") {
Alert.show("登陸失敗: " + result[1]);
} else if (flag == "1") {
Alert.show("登陸成功: " + result[1]);
} else if (flag == "-1") {
Alert.show("異常: " + result[1]);
}
}
public function faultHandler(event: FaultEvent):void
{//出錯處理
Alert.show("sorry,出錯了!!!");
}
}
二,PHP服務(wù)器端
1,將amfphp文件夾置于MyTest項目的根目錄下,打開瀏覽器輸入下述地址驗證amfphp是否安裝成功
復制代碼 代碼如下:
http://localhost/MyTest/amfphp/gateway.php
amfphp就是通過這個gateway來定位我們的服務(wù)類,并將請求轉(zhuǎn)發(fā)給這些服務(wù)類進行處理的。
2,Login.php文件,包含了處理登陸請求的Login類,此文件置于BusinessLogic目錄下
代碼
復制代碼 代碼如下:
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,將globals.php中的服務(wù)路徑項修改如下,為amfphp指明服務(wù)類所在的目錄
復制代碼 代碼如下:
$servicesPath = "../BusinessLogic/";
作者:洞庭散人
AMFPHP 下載地址

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

PHPmanagessessionsandcookiestomaintainstateacrossHTTPrequests.1.Sessionsstoredataserver-side,usingauniquesessionIDstoredtypicallyinacookie(PHPSESSID).2.Cookiesstoredataclient-side,setviasetcookie()andaccessedthrough$_COOKIE.3.Sessionsaresaferforsensi

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

Use prepared statements in PHP mainly to prevent SQL injection attacks, improve performance, make the code clearer and easier to debug. 1. It effectively prevents SQL injection through parameterized queries, ensuring that user input is always processed as data rather than SQL logic; 2. Preprocessing statements only need to be compiled once when executed multiple times, significantly improving execution efficiency, especially suitable for batch operations; 3. Parameter binding supports position and named placeholders, separates SQL and data, and enhances code readability and maintenance; 4. Errors can be exposed in advance in the prepare stage, and exceptions can be handled uniformly by setting error mode, which helps to quickly debug.
