PHPRPC for PHP

來源:互聯網
上載者:User
PHPRPC 是一個輕型的、安全的、跨網際的、跨語言的、跨平台的、跨環境的、跨域的、支援複雜物件傳輸的、支援引用參數傳遞的、支援內容輸出重新導向的、支援分級錯誤處理的、支援會話的、面向服務的高效能遠端程序呼叫協議。
下載地址:http://www.phprpc.org/zh_CN/download/
該版本直接解壓後就可以使用,其中
屬於公用檔案。不論是用戶端還是伺服器端都需要這些檔案。
是用戶端檔案,如果你只需要使用用戶端,那麼只要有上面那些公用檔案和這個檔案就可以使用了,使用時,直接在你的程式中包含 phprpc_client.php 就可以,公用檔案不需要單獨包含。
這三個檔案是伺服器端需要的檔案。
其中 dhparams 目錄中包含的是加密傳輸時用來產生密鑰的參數
dhparams.php 是用來讀取 dhparams 目錄中檔案的類。
phprpc_server.php 是伺服器端,如果你要使用 PHP 來發布 PHPRPC 服務,只需要包含這個檔案就可以了。公用檔案和 dhparams.php 都不需要單獨包含。
PHP 4.3+、PHP 5、PHP 6
用戶端要求開啟 socket 擴充。
伺服器端需要有 IIS、Apache、lighttpd 等可以運行 PHP 程式的 Web 服務器。
如果伺服器端需要加密傳輸的能力,必須要保證 session 配置正確。
include('php/phprpc_server.php'); //負載檔案
function hello($name) {
return'Hello ' . $name;
}
$server = new PHPRPC_Server(); //建立服務端
$server->add(array('hello', 'md5', 'sha1')); //數組形式一次註冊多個函數
$server->add('trim'); //單一註冊
$server->start(); //開啟服務
?>
include ("php/phprpc_client.php"); //負載檔案
$client = new PHPRPC_Client('http://127.0.0.1/server.php'); //建立用戶端 並串連服務端檔案
echo$client->Hello("word"); //調用方法 返回 hello word
?>
-------------------------------------------------- --------------------------------------------------- ------------------------------
服務端其他說明:
include('php/phprpc_server.php'); //負載檔案
function hello($name) {
return'Hello ' . $name;
}
class Example1 {
staticfunction foo() {
return'foo';
}
function bar() {
return'bar';
}
}
$server = new PHPRPC_Server(); //建立服務端
$server->add('foo', 'Example1'); //靜態方法直接調用
$server->add('bar', new Example1()); //非靜態方法 需要執行個體化
//註冊別名調用
$server->add('hello', NULL, 'hi'); //第三參數是函數的別名 用戶端通過別名來調用函數
$server->add('foo', 'Example1', 'ex1_foo');
$server->add('bar', new Example1(), 'ex1_bar');
$server->setCharset('UTF-8'); //設定編碼
$server->setDebugMode(true); //列印錯誤
$server->setEnableGZIP(true); //啟動壓縮輸出雖然可以讓傳輸的資料量減少,但是它會佔用更多的記憶體和 CPU,因此它預設是關閉的。
$server->start(); //開啟服務
?>
-------------------------------------------------- --------------------------------------------------- ---------------------------
用戶端其他說明:
include ("php/phprpc_client.php");
$client = new PHPRPC_Client();
$client->useService('http://127.0.0.1/server.php'); //遠程調用地址
$client->setKeyLength(1000); //密鑰長度
$client->setEncryptMode(3); //加密等級0-3
$client->setCharset('UTF-8'); //設定編碼
$client->setTimeout(10); //設定逾時時間
echo$client->hi('PHPRPC'), "\r\n"; //調用函數
echo$client->getKeyLength(), "\r\n"; //下面是傳回值
echo$client->getEncryptMode(), "\r\n";
echo$client->getCharset(), "\r\n";
echo$client->getTimeout(), "\r\n";
?>
-------------------------------------------------- --------------------------------------------------- ----------------------
關於session
include('php/phprpc_server.php');
class ExampleCounter {
function ExampleCounter() {
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
}
}
function inc() {
$_SESSION['count'] += 1;
}
functioncount() {
return$_SESSION['count'];
}
}
$server = new PHPRPC_Server();
$server->add(array('inc', 'count'), new ExampleCounter());
$server->start();
?>
include("php/phprpc_client.php");
$client = newPHPRPC_Client();
$client->useService('http://127.0.0.1/1.php');
$client->setTimeout(10);
echo $client->inc();
echo $client->count();
echo $client->inc();
echo $client->count();
?>
每次重新整理都是建立的client 服務端並不能識別.
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.