,
Hprose 2.0 for PHP 文档: https://github.com/hprose/hprose-php
php服务端 放入/public
<?php
//define('ENTRY',microtime(true));
require __DIR__ . '/../vendor/autoload.php';
//require_once '../vendor/hprose/hprose/src/Hprose.php';
use Hprose\Http\Server;
function hello($name,$a,$b) {
//return microtime(true)-ENTRY;
return $name.' hello world! '."$a $b";
}
$server = new Server();
$server->addFunction('hello');
$server->start();php客户端与服务端放不同服务器 thinkphp6 放入/public 远程调用hello 输出 zhang hello world! param1 param2
<?php
require __DIR__ . '/../vendor/autoload.php';
$client = new \Hprose\Http\Client('http://tp609.cc/hprose.php', false);
echo $client->hello('zhang','param1','param2');html5客户端 与php客户端类似 alert输出zhang hello world! aa bb hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js
<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
var client = new hprose.HttpClient("http://tp609.cc/hprose.php", ["hello"]);
client.hello("zhang",'aa','bb', function(result) {
alert(result);
}, function(name, err) {
alert(err);
});
</script>
</body>swoole http服务端,命令运行 php hprose_swoole_http_server.php
<?php
require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return "Hello $name!";
}
$server = new Server("http://0.0.0.0:8081");
$server->setErrorTypes(E_ALL);
$server->setDebugEnabled();
$server->setCrossDomainEnabled();
$server->addFunction('hello');
$server->start();html5连接测试 hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js
<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
var client = new hprose.HttpClient("http://127.0.0.1:8081", ["hello"]);
client.hello("zhang", function(result) {
alert(result);
}, function(name, err) {
alert(err);
});
</script>
</body>alert回显正确

swoole websocket服务端,命令运行 php hprose_swoole_websocket_server.php
<?php
require_once "../vendor/autoload.php";
use Hprose\Swoole\Server;
function hello($name) {
return 'Hello ' . $name;
}
$server = new Server('ws://0.0.0.0:8082/');
$server->addFunction('hello');
$server->start();html5连接测试,访问后console显示Hello World hprose-html5.min.js位于https://gitee.com/andot/hprose-html5/blob/master/dist/hprose-html5.min.js
<html>
<head>
<script type="text/javascript" src="static/hprose-html5.min.js"></script>
</head>
<body>
<script type="text/javascript">
(function() {
'use strict';
var client = hprose.Client.create('ws://127.0.0.1:8082', ['hello']);
client.ready(function(stub) {
stub.hello.idempotent = true;
stub.hello('World')
.then(function(result) {
console.info(result);
},function(e) {
console.error(e);
});
},
function(e) {
console.error(e);
});
})();
</script>
</body>