在你的 composer 項目中的 composer.json 文件中,添加這部分:
"require": {
"hprose/hprose": ">=2.0.0"
}
就可以了衩辟。
使用composer update 命令更新擴展包
1.在服務器端在app目錄下 創(chuàng)建Services目錄(可手動創(chuàng)建)
創(chuàng)建UserService.php
public function init(){
addMethod('test',$this); //添加test方法 傳入實例化對象牲阁,則是添加實例化方法
//$server->addMethod('test', 'UserService'); //傳入類名 則是添加靜態(tài)方法
$server->start();
}
public function test(){
return 'hello';
}
路由設置励烦,在api路由中添加
Route::post('test', function (Request $request) {
$server = new \App\Services\UserService();
$server->init(); //開啟服務
})->middleware('api');
2.在客戶端遠程調用,同樣安裝擴展包之后
public function index(Request $request){
//服務端路由在api路由中配置渡讼,則此處路由應加上api/test
//實例化可選參數(shù) 加上false 即創(chuàng)建創(chuàng)建一個同步的 HTTP 客戶端
//不寫false 為創(chuàng)建一個異步的 HTTP 客戶端
$user =new Client('http://127.0.0.1:81/api/test',false);
$res=$user->test();
return $res;
}
需要注意的是,異步的HTTP 客戶端是不能往外返回值的
可以使用靜態(tài)方法來創(chuàng)建
$client = Client::create('http://localhost:81/api/test');
$client->test()->then(function($result){
echo $result;
});
這樣 會直接輸出:hello