ThinkPHP3.0如何实现Hprose,整体项目已经开发差不多,现在需要开启远程调用。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThinkPHP3.0如何实现Hprose,整体项目已经开发差不多,现在需要开启远程调用。相关的知识,希望对你有一定的参考价值。
计划方案是在现有代码的基础上将Hprose的入口开放文件和TP的入口文件合并,如何保证在TP框架搭建完成后先不返回请求的URL,将server端搭建好后再运行返回,实现远程调用
参考技术A 这是水贴,不是技术贴hprose rpc使用实例(同时有Java和Delphi客户端的例子)
php server
- <?php
- require_once(‘src/Hprose.php‘);
- function hello($name) {
- echo "Hello $name!";
- return "Hello $name!";
- }
- function e() {
- throw new Exception("I am Exception");
- }
- function ee() {
- require("andot");
- }
- function asyncHello($name, $callback) {
- sleep(3);
- $callback("from server,Hello async $name!");
- }
- $server = new HproseHttpServer();
- $server->setErrorTypes(E_ALL);
- $server->setDebugEnabled();
- $server->addFunction(‘hello‘);
- $server->addFunctions(array(‘e‘, ‘ee‘));
- $server->addAsyncFunction(‘asyncHello‘);
- $server->addFilter(new HproseJSONRPCServiceFilter());
- $server->start();
php client
- <?php
- require_once (‘src/Hprose.php‘);
- $test = new HproseHttpClient("http://localhost/hprose/http_server.php");
- echo ‘<br/>‘;
- // var_dump($test->invoke("hello", $args, 0, HproseResultMode::Serialized, 0));
- echo ‘<br/>‘;
- // var_dump($test->invoke("hello", $args, 0, HproseResultMode::Raw, 0));
- echo ‘<br/>‘;
- // var_dump($test->invoke("hello", $args, 0, HproseResultMode::RawWithEndTag, 0));
- echo $test->asyncHello("WORLD");
- echo ‘<br/>‘;
- $test->asyncHello("WORLD", function ($result)
- {
- echo "from client result: ";
- var_dump($result);
- });
- echo ‘<br/>‘;
- /*
- $test->dnslookup("www.baidu.com", function($result, $args) {
- echo "result: ";
- var_dump($result);
- echo "args: ";
- var_dump($args);
- });
- */
- ?>
java client
- package hprose.hello.client;
- import hprose.client.HproseHttpClient;
- import java.io.IOException;
- public class HelloClient {
- public static void main(String[] args) throws IOException {
- HproseHttpClient client = new HproseHttpClient();
- client.useService("http://10.0.0.100:9090/hprose_demo/Hello");
- String result = (String) client.invoke("sayHello", new Object[] { "Hprose" });
- System.out.println(result);
- result = (String) client.invoke("sayHello", new Object[] { "中国" });
- System.out.println(result);
- System.out.println(client.invoke("add",new Object[]{115,316}));
- }
- }
- package hprose.hello.client;
- import java.io.IOException;
- import hprose.client.HproseHttpClient;
- public class TestPHPClient {
- public static void main(String[] args) throws Exception {
- HproseHttpClient client = new HproseHttpClient();
- client.useService("http://10.0.0.105/hprose/http_server.php");
- String result = (String) client.invoke("hello",
- new Object[] { "Hprose来自Java客户端" });
- System.out.println(result);
- System.out.println(client.invoke("asyncHello",new Object[] { "Hprose来自Java客户端"}));
- }
- }
delphi client:
- unit Unit4;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, HproseHttpClient, HproseClient;
- type
- TForm4 = class(TForm)
- Button1: TButton;
- HproseHttpClient1: THproseHttpClient;
- Button2: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form4: TForm4;
- implementation
- {$R *.dfm}
- procedure TForm4.Button1Click(Sender: TObject);
- begin
- HproseHttpClient1.UseService(‘http://10.0.0.100:9090/hprose_demo/Hello‘);
- ShowMessage(HproseHttpClient1.Invoke(‘sayHello‘, [‘World中文‘]));
- ShowMessage(HproseHttpClient1.Invoke(‘add‘, [15,63]));
- end;
- //https://raw.githubusercontent.com/andot/hprose/master/doc/1.3/docx/pascal.docx
- procedure TForm4.Button2Click(Sender: TObject);
- begin
- HproseHttpClient1.UseService(‘http://10.0.0.105/hprose/http_server.php‘);
- ShowMessage(HproseHttpClient1.Invoke(‘hello‘, [‘World时间‘]));
- ShowMessage(HproseHttpClient1.Invoke(‘asyncHello‘, [‘xxx‘]));
- end;
- end.
源码工程:http://pan.baidu.com/s/1o6vF1qE
http://blog.csdn.net/earbao/article/details/46501671
以上是关于ThinkPHP3.0如何实现Hprose,整体项目已经开发差不多,现在需要开启远程调用。的主要内容,如果未能解决你的问题,请参考以下文章
基于 hprose/hprose-php 开发的 Laravel 扩展:laravel-hprose