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端
<?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://blog.csdn.net/earbao/article/details/46428565
源码下载:http://pan.baidu.com/s/1eQ6KOcq
phprpc 使用实例(例实没错却不能执行)函数冲突gzdecode的更多相关文章
- phprpc 使用实例(同时有Java、Android和Delphi客户端的例子)
PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出重定向的.支持分级错误处理的.支持会话的.面向服务的高性能远程过 ...
- 简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值(转)
本文将简单介绍如何使用PowerMock和Mockito来mock1. 构造函数2. 静态函数3. 枚举实现的单例4. 选择参数值做为函数的返回值5. 在调用mock出来的方法中,改变方法参数的值 一 ...
- Python_Selenium 之以login_page为例实现对basepage封装好的方法调用和对common中公共方法的调用
目的:简化代码,提供框架该有的东西每一个函数 -提供了一个功能 - 公共的功能有了basepage,在PageObjects当中直接调用元素操作. 以下以login_page 为例,实现从配置文件中读 ...
- 图解“管道过滤器模式”应用实例:SOD框架的命令执行管道
管道和过滤器 管道和过滤器是八种体系结构模式之一,这八种体系结构模式是:层.管道和过滤器.黑板.代理者.模型-视图-控制器(MVC) 表示-抽象-控制(PAC).微核.映像. 管道和过滤器适用于需要渐 ...
- 【COCOS2D-HTML5 开发之三】演示样例项目附源代码及执行的GIF效果图
本站文章均为李华明Himi原创,转载务必在明显处注明:(作者新浪微博:@李华明Himi) 转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/cocos2d- ...
- phprpc的使用示例以及报错Fatal error: Cannot redeclare gzdecode() in D:\wamp\www\immoc\phprpc\compat.php 处理
今天看书,发现了PHPRPC这个好东东,故在此写下来以作笔记. PHPRPC 是一个轻型的.安全的.跨网际的.跨语言的.跨平台的.跨环境的.跨域的.支持复杂对象传输的.支持引用参数传递的.支持内容输出 ...
- 【转】C#父类与子类的静态成员变量、实例成员变量、构造函数的执行顺序
原文地址:http://www.xuebuyuan.com/1092603.html Win7+VS2010测试的结果如下: ①子类静态成员变量②子类静态构造函数③子类实例成员变量④父类静态成员变量⑤ ...
- 解决win7系统不支持16位实模式汇编程序DOS执行的问题
这学期学习了汇编,在自己电脑上发现,win7的dos不支持16位实模式. 对编程来说,不能执行程序是致命的. 在经过网上搜集资料后,得到一种解决的方法--使用dosbox软件执行 dosbox简单说, ...
- C++入门经典-例9.1-函数模板,函数模板的作用,使用数组作为模板参数
1:函数模板不是一个实在的函数,因此编译器不能为其生成可执行的代码.定义函数模板只是一个对函数功能框架的描述,在具体执行时,将根据传递的实际参数决定其功能. 2:函数模板定义的一般形式如下: temp ...
随机推荐
- ubuntu下环境变量
01:/etc/environment:是设置整个系统的环境: 02:/etc/profile:是设置所有用户的环境: 03:.bashrc :本地用户:
- 本地计算机上的MSSQLSERVER服务启动后又停止了。一些服务自动停止,如果它们没有什么可做的
本地计算机上的MSSQLSERVER服务启动后又停止了.一些服务自动停止,如果它们没有什么可做的 笔者ASP运行环境:操作系统为Windows XP SP2,IIS的版本为默认的5.1,数据库为SQL ...
- java学习容器
自己模拟ArrayList: private Object[] elementData; private int size; // 可以指定集合大小,默认10 public MyArrayList(i ...
- java--4种内部类
内部类: 一 非静态内部类 //非静态内部类 //非静态内部类可任意调用外部类的局部变量,无论是否private //在外部类中要实例化内部类:InnerClass inner = new Inner ...
- 样式其他与JS脚本语言
样式其他:display(显示block和隐藏none,不占位置) visibility(显示visible和隐藏hidden,占位置) overflow(超出范围 hidden隐藏) 透明(op ...
- 最小化安装centos的init初始化脚本
#!/bin/bash #this script is appropriate .x(这脚本适合最小化安装6.x版本的系统) #you have already install the os read ...
- android sdk 国内镜像地址
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- easyui表单插件-包括日期时控件-列表
← jQuery EasyUI 表单插件 – Numberspinner 数值微调器 jQuery EasyUI 表单插件 - Timespinner 时间微调器 jQuery EasyUI 插件 ...
- POJ 3320 Jessica's Reading Problem 尺取法
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- 如何通过Button获取UITableViewCell
发现一个奇怪的问题: 手机(ios7) 2015-06-17 15:11:29.323 ***[1412:60b] [btn superview] = UITableViewCellContent ...