phprpc 使用实例(例实没错却不能执行)函数冲突gzdecode
PHPRPC 是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。
遇到的问题总结:
Fatal error: Cannot redeclare gzdecode() in
1、重命名compat.php、phprpc_client.php的gzdecode(和系统函数冲突)函数为gzdecode_other
Non-static method PHPRPC_Server::initSession() should not be called statically
2、修改phprpc_server.php文件里面initSession函数的类型为static即可。
3、phprpc_client.php 472行警告:
Notice: Undefined offset in phprpc_client.php line 472
修改成
$arr = explode('=', $c, 2);
if(count($arr)>1)
{
list($name, $value)=$arr;
if (!in_array($name, array('domain', 'expires', 'path', 'secure'))) {
$_PHPRPC_COOKIES[$name] = $value;
}
}
4、Warning: The magic method __get() must have public visibility and cannot be static in compat.php(235) : eval()'d code on line 3
改成public:
eval('
class ' . $classname . ' {
public function __get($name) {
php server端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php include ( 'php/phprpc_server.php' ); include 'entitys.php' ; function hello( $name ) { return 'Hello ' . $name ; } $server = new PHPRPC_Server(); $server ->setCharset( 'UTF-8' ); $server ->setDebugMode(true); $server ->setEnableGZIP(true); $server ->add( 'hello' ); $server ->add( 'sort' ); //$server->add(array('hello','md5','sha1')); $server ->add( 'getcwd' ); //导出php内置函数 $server ->add( 'add' , new Calculator()); $server ->add( 'mul' , new Calculator()); $server ->add( 'div' , new Calculator()); $server ->add( 'sub' , new Calculator()); $server ->add( 'fuck' , new Calculator()); $server ->add( 'getstudent' , new Calculator()); $server ->add( 'getstudents' , new Calculator()); $server ->start(); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | <?php class Student { public $id ; public $name ; public $age ; } /** * Calculator - sample class to expose via JSON-RPC */ class Calculator { /** * Return sum of two variables * * @param int $x * @param int $y * @return int */ public function add( $x , $y ) { return $x + $y ; } /** * Return difference of two variables * * @param int $x * @param int $y * @return int */ public function sub( $x , $y ) { return $x - $y ; } /** * Return product of two variables * * @param int $x * @param int $y * @return int */ public function mul( $x , $y ) { return $x * $y ; } /** * Return the division of two variables * * @param int $x * @param int $y * @return float */ public function div( $x , $y ) { return $x / $y ; } public function fuck( $str , $x , $y ) { $m = $x + $y ; return 'fuck 日本' . $str . $m ; } public function getstudent( $student ) { $student = new Student(); $student ->id = 110; $student ->name = 'php' ; $student ->age = 21; return $student ; } public function getstudents( $id ) { $entitys = array (); for ( $i =0; $i <10; $i ++) { $student = new Student(); $student ->id = $i ; $student ->name = '中文php' . $i ; $student ->age = 21; array_push ( $entitys , $student ); } return $entitys ; } } ?> |
php client客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?php include ( "php/phprpc_client.php" ); $client = new PHPRPC_Client( 'http://localhost/phprpc/index.php' ); echo '<br/>' ; $ret = $client ->add(12,32); print_r( $ret ); echo '<br/>' ; $ret = $client ->mul(12,32); print_r( $ret ); echo '<br/>' ; $ret = $client ->fuck( 'xx测试' ,12,32); print_r( $ret ); echo '<br/>' ; $ret = $client ->hello( 'xx测试' ,12,32); print_r( $ret ); echo '<br/>' ; $ret = $client ->getstudent( 'getstudent' ); print_r( $ret ); echo '<br/>' ; $ret = $client ->getstudents( 'getstudents' ); print_r( $ret ); echo '<br/>' ; print_r( $client ->getOutput()); ?> |
java 客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package com.jiepu.client; import org.phprpc.PHPRPC_Client; import org.phprpc.util.AssocArray; import org.phprpc.util.Cast; public class IncClient { // http://leyteris.iteye.com/blog/1004669 //http://www.phprpc.org/zh_CN/docs/ //android下面也一样 public static void main(String[] args) { PHPRPC_Client client = new PHPRPC_Client( "http://10.0.0.108/phprpc/index.php" ); System.out.println(client.invoke( "add" , new Object[] { 12, 32 })); System.out.println(client.invoke( "sub" , new Object[] { 12, 32 })); System.out.println(client.invoke( "mul" , new Object[] { 12, 32 })); System.out.println(client.invoke( "div" , new Object[] { 12, 32 })); System.out.println(Cast.toString(client.invoke( "fuck" , new Object[] { "hehe你好" , 12, 32 }))); System.out.println(Cast.toString(client.invoke( "getcwd" , new Object[] {}))); System.out.println(Cast.toString(client.invoke( "hello" , new Object[] { "java" }))); //public公共类Student自动映射转换 Object obj=client.invoke( "getstudent" , new Object[] { "java" }); Student stu=(Student) Cast.cast(obj, Student. class ); System.out.println(stu); /**手动转换 HashMap<Object, Object> objs = (HashMap<Object, Object>) client.invoke("getstudent", new Object[] { "java" }); if(objs.size()>0) { Student student = new Student(); student.setAge(Integer.parseInt(Cast.toString(objs.get("age")))); student.setName(Cast.toString(objs.get("name"))); student.setId(Integer.parseInt(Cast.toString(objs.get("id")))); System.out.println(student); System.out.println("==========="); }*/ AssocArray alist = (AssocArray) client.invoke( "getstudents" , new Object[] { "java" }); for (int i = 0; i < alist.size(); ++i) { // System.out.println(alist.get(i).getClass()); Student student=(Student) Cast.cast(alist.get(i), Student. class ); System.out.println(student); } // System.out.println(client.getWarning()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | package com.jiepu.client; import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 3475272786622279878L; private Integer id; private String name; private Integer age; @Override public String toString() { return "Student [age=" + age + ", id=" + id + ", name=" + name + "]" ; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } |
android客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | package com.example.androidjsonrpc; import java.util.List; import org.alexd.jsonrpc.JSONRPCClient; import org.alexd.jsonrpc.JSONRPCException; import org.alexd.jsonrpc.JSONRPCParams.Versions; import org.phprpc.PHPRPC_Client; import org.phprpc.util.AssocArray; import org.phprpc.util.Cast; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import com.alibaba.fastjson.JSON; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void run(View view) { new Thread( new Runnable() { @Override public void run() { runinthread(); } }).start(); } public void runphprpc(View view) { new Thread( new Runnable() { @Override public void run() { runphprpc(); } }).start(); } private void runphprpc() { PHPRPC_Client client = new PHPRPC_Client( "http://10.0.0.108/phprpc/index.php" ); System.out.println(client.invoke( "add" , new Object[] { 12, 32 })); System.out.println(client.invoke( "sub" , new Object[] { 12, 32 })); System.out.println(client.invoke( "mul" , new Object[] { 12, 32 })); System.out.println(client.invoke( "div" , new Object[] { 12, 32 })); System.out.println(Cast.toString(client.invoke( "fuck" , new Object[] { "hehe你好" , 12, 32 }))); System.out.println(Cast.toString(client.invoke( "getcwd" , new Object[] {}))); System.out.println(Cast.toString(client.invoke( "hello" , new Object[] { "java" }))); //public公共类Student自动映射转换 Object obj=client.invoke( "getstudent" , new Object[] { "java" }); Student stu=(Student) Cast.cast(obj, Student. class ); System.out.println(stu); /**手动转换 HashMap<Object, Object> objs = (HashMap<Object, Object>) client.invoke("getstudent", new Object[] { "java" }); if(objs.size()>0) { Student student = new Student(); student.setAge(Integer.parseInt(Cast.toString(objs.get("age")))); student.setName(Cast.toString(objs.get("name"))); student.setId(Integer.parseInt(Cast.toString(objs.get("id")))); System.out.println(student); System.out.println("==========="); }*/ AssocArray alist = (AssocArray) client.invoke( "getstudents" , new Object[] { "java" }); for (int i = 0; i < alist.size(); ++i) { // System.out.println(alist.get(i).getClass()); Student student=(Student) Cast.cast(alist.get(i), Student. class ); System.out.println(student); } } public void runinthread() { // https://code.google.com/p/android-json-rpc/downloads/list // http://www.oschina.net/p/android-json-rpc JSONRPCClient client = JSONRPCClient.create( "http://10.0.0.107/json_server/server.php" , Versions.VERSION_2); client.setConnectionTimeout(2000); client.setSoTimeout(2000); try { String string = client.callString( "fuck" , "android谷歌" , 15, 32); Log.i( "androidjsonrpc" , "fuck=" + string); int i = client.callInt( "add" , 56, 25); Log.i( "androidjsonrpc" , i + "" ); // Student student=(Student) client.call("getstudent", new // Student(1,"name",123)); // Log.i("androidjsonrpc", student.toString()); // Log.i("androidjsonrpc", client.call("getstudent", new // Student(1,"name",123)).toString()); // Log.i("androidjsonrpc", client.call("getstudents", // "xx").toString()); String str = client.callString( "getstudent" , new Student(1, "name" , 123)); Log.i( "androidjsonrpc" , str); // fastjson 转换json字符串为对象 Student student = JSON.parseObject(str, Student. class ); Log.i( "androidjsonrpc" , student.toString()); str = client.callString( "getstudents" , "xx" ); Log.i( "androidjsonrpc" , str); // 使用到fastjson 转换json数组为list对象 List<Student> students = JSON.parseArray(str, Student. class ); Log.i( "androidjsonrpc" , students.toString()); } catch (JSONRPCException e) { e.printStackTrace(); } } } |
Delphi 客户端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | unit Unit4; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, PHPRPCSynaHttpClient, PHPRPC, PHPRPCClient, PHPRPCIdHttpClient, StdCtrls; type TForm4 = class (TForm) Button1: TButton; PHPRPCIdHttpClient1: TPHPRPCIdHttpClient; PHPRPCSynaHttpClient1: TPHPRPCSynaHttpClient; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end ; TStudent = class (TPHPObject) private Fid: Integer; Fname: AnsiString; Fage: Integer; published property id: Integer read Fid write Fid; property name: AnsiString read Fname write Fname; property age: Integer read Fage write Fage; end ; var Form4: TForm4; implementation { $R *.dfm} procedure TForm4.Button1Click(Sender: TObject); var clientProxy: Variant; int_ret, i: Integer; string_ret: string; vhashmap: Variant; ohashmap: THashMap; arraylist, keys, values: TArrayList; key, value, tmp: Variant; student: TStudent; begin // http://www.phprpc.org/zh_CN/docs/ clientProxy := PHPRPCIdHttpClient1.useService ( 'http://10.0.0.108/phprpc/index.php' ); int_ret := clientProxy.add(12, 52); ShowMessage(IntToStr(int_ret)); // 中文字符串参数要转换为UTF8编码 string_ret := clientProxy.fuck(AnsiToUTF8( 'xxx你好' ), 15, 45); // 中文的服务器输出的也是UTF8编码,本地打印要转换为本地编码 ShowMessage(UTF8ToAnsi(string_ret)); string_ret := clientProxy. getcwd (); ShowMessage(UTF8ToAnsi(string_ret)); string_ret := clientProxy.hello(AnsiToUTF8( 'xxx' )); ShowMessage(UTF8ToAnsi(string_ret)); TStudent.RegisterClass( 'Student' ); tmp := clientProxy.getstudent(AnsiToUTF8( 'java' )); // ShowMessage(UTF8ToAnsi(tmp)); // 转换为student对象 student := TStudent(TStudent.FromVariant(tmp)); ShowMessage(IntToStr(student.id) + ',' + UTF8ToAnsi(student.name)+ ',' + IntToStr (student.age)); vhashmap := clientProxy.getstudents(AnsiToUTF8( 'java' )); ohashmap := THashMap(THashMap.FromVariant(vhashmap)); arraylist := ohashmap.ToArrayList; for i := 0 to arraylist. Count - 1 do begin // ShowMessage(arraylist.Items[i]); student := TStudent(TStudent.FromVariant(arraylist.Items[i])); ShowMessage(IntToStr(student.id) + ',' + UTF8ToAnsi(student.name)+ ',' + IntToStr (student.age)); end ; arraylist.Free; { keys:=ohashmap.Keys; values:=ohashmap.Values; for i := 0 to keys. Count - 1 do begin key:=keys[i]; value:=values[i]; ShowMessage(values.List[i]); ShowMessage(value); // ohashmap := THashMap(THashMap.FromVariant(value)); //ShowMessage(ohashmap.Values[0].Properties['name']); end ; } end ; end . |
来自:http://blog.csdn.net/earbao/article/details/46428565
源码下载:http://pan.baidu.com/s/1eQ6KOcq
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理