码迷,mamicode.com
首页 > Web开发 > 详细

phprpc 使用实例(例实没错却不能执行)函数冲突gzdecode

时间:2015-09-21 12:05:34      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/achengmu/p/4825465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!