phprpc 使用实例(同时有Java、Android和Delphi客户端的例子)
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端
- <?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();
- ?>
- <?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客户端
- <?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 客户端:
- 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());
- }
- }
- 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客户端:
- 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 客户端:
- 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://pan.baidu.com/s/1eQ6KOcq
http://blog.csdn.net/earbao/article/details/46428565
phprpc 使用实例(同时有Java、Android和Delphi客户端的例子)的更多相关文章
- hprose rpc使用实例(同时有Java和Delphi客户端的例子)
php server <?php require_once('src/Hprose.php'); function hello($name) { echo "Hello $name!& ...
- java android 访问DELPHI 的DATASNAP
最新版的DELPHI开发DATASNAP非常简单便捷,DataSnap的REST风格和对JSON的支持,使之成为服务器端开发的神器. 一.DATASNAP服务器中的方法: TServerMethods ...
- Atitti.java android反编译解决方案-----虚拟机方案
Atitti.java android反编译解决方案-----虚拟机方案 哈哈,终极解决方案是虚拟机...c++也可以反编译为汇编代码,但无需担心,因为读懂汇编太麻烦..只要不能拿到c++源码就可.. ...
- Java Android HTTP实现总结
Java Android HTTP实现总结 Http(Hypertext Transfer Protocol)超文本传输协议,是一个基于请求/响应模式的无状态的协议,Http1.1版给出了持续连接的机 ...
- 性能优化之Java(Android)代码优化
最新最准确内容建议直接访问原文:性能优化之Java(Android)代码优化 本文为Android性能优化的第三篇——Java(Android)代码优化.主要介绍Java代码中性能优化方式及网络优化, ...
- phprpc 使用实例(例实没错却不能执行)函数冲突gzdecode
PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出重定向的.支持分级错误处理的.支持会话的.面向服务的高性能远程过 ...
- Java/Android引用类型及其使用分析
Java/Android中有四种引用类型,分别是: Strong reference - 强引用Soft Reference - 软引用Weak Reference - ...
- Java Android 注解(Annotation) 及几个常用开源项目注解原理简析
不少开源库(ButterKnife.Retrofit.ActiveAndroid等等)都用到了注解的方式来简化代码提高开发效率. 本文简单介绍下 Annotation 示例.概念及作用.分类.自定义. ...
- [Java][Android][Process] 暴力的服务能够解决一切,暴力的方式运行命令行语句
不管是在Java或者Android中运行命令行语句殊途同归都是创建一个子进程运行调用可运行文件运行命令.类似于Windows中的CMD一样. 此时你有两种方式运行:ProcessBuilder与Run ...
随机推荐
- keepavlied一些参数
priority 表示优先级 virtual_ipaddress 虚拟的IP地址(VIP) delay_loop 每个2秒检查一次real_server状态 notify_down 检测到服务down ...
- J2EE应用服务器计数器
常用的J2EE应用服务器包括Weblogic.WebSphere和Tomcat等,以下是以WebLogic为例给出的计数器. 类别 计数器名称 计数器描述 JVM Heap Size JVM堆大小,该 ...
- CSS:重量和级联规则,确定其优先级
资源:http://www.ido321.com/1063.html 首先,给大家看一篇关于CSS优先级的演示样例:http://www.ido321.com/76.html 一.主要的优先级规则 比 ...
- 面试中关于Java中涉及到知识点(转)
本篇文章会对面试中常遇到的Java技术点进行全面深入的总结,帮助我们在面试中更加得心应手,不参加面试的同学也能够借此机会梳理一下自己的知识体系,进行查漏补缺. 1. Java中的原始数据类型都有哪些, ...
- USB基础简介
一.USB2.0 Universal Serial Bus (通用串行总线) 符合USB总线数据通信要求的通信协议 1.意义 1.易用(热插拔.即插即用) 2.易扩充(USBHub可同时操作127个 ...
- getAttribute()获取属性
Js:getAttribute[转] 一份文档就是一棵节点树. ●节点分为不同的类型:元素节点.属性节点和文本节点等. ●getElementById()方法将返回一个对象,该对象对应着文档里的一个特 ...
- 《C陷阱与缺陷》整理二
1.数组名作实參 在C语言中,我们没有办法将一个数组作为函数參数传递,假设我们使用数组名作为參数.这个时候数组名立马会被转换为指向该数组的第一个元素的指针. 关于这一点的理解能够向前深 ...
- python算法之二分查找
说明:大部分代码是在网上找到的,好几个代码思路总结出来的 通常写算法,习惯用C语言写,显得思路清晰.可是假设一旦把思路确定下来,并且又不想打草稿.想高速写下来看看效果,还是python写的比較快.也看 ...
- Juuluu 旗下企业站点管理系统3.0.1公布!
KgE金刚企业站点管理系统是广州聚龙软件为国内中小企业开发的一款企业站点管理系统,KgE採用可视化的标签模型,可在Dreamvear等网页编辑下可视化编辑,KgE使用javaeemsyqlyuijqu ...
- Android网络:开发浏览器(二)——功能完善之长按网页图片菜单
上述的历史和书签的功能已经实现.不过如果我们长时间按住图片,并不会出现如同UC中的一系列选项,我们可以来看看UC中的长按图片出现的菜单. 图10.2.9 UC中的长按图片菜单 我们可以看到UC中 ...