每個服務器上都安裝yar擴展
然后要被訪問的服務器上寫一段代碼,開啟rpc 服務器
index控制器index方法里:
~~~
if($_SERVER['PHP_SELF'] == '/nmp_remote.php'){
$this->yar();
return '';
}
public function yar(){
? ? ? ? $server = new \Yar_Server(new YarApi);
? ? ? ? return $server->handle();
}
~~~
~~~
namespace app\common\lib;
Class YarApi{
/**
* 調用內部函數
* @param? string $name? ?函數名
* @param? array? $params 參數 沒有傳[]
* @return mixed? ? ? ? ?返回結果
*/
public function function_call($name, $params=[]){
if(function_exists($name)){
return call_user_func_array($name, $params);
}else{
return 'function not exists';
}
}
/**
* 調用類方法
* @param? string $className 類命名空間
* @param? string $method? ? 方法名
* @param? array? $params? ? 參數 沒有傳[]
* @return mixed? ? ? ? ? ? 返回結果
*/
public function class_call($className, $method, $params=[]){
if(method_exists($className, $method)){
return call_user_func_array([$className, $method], $params);
}else{
return 'method not exists';
}
}
}
~~~
暴露的是 server里傳入的類 的方法
這兩個方法其實用于調用函數和類方法 、 類方法支持靜態。
然后 其他模塊里訪問:
~~~
$client = new \yar_client("http://nmp.cn/nmp_remote.php");
$ret = $client->function_call('build_part_commission_table', [['user_id'=>1, 'finish_time'=>'2017-10-28 10:00:00']]);
$data = [];
$ret2 = $client->class_call('\aa', 'sel', ['a']);
}
~~~

暴露的是 server里傳入的類 的方法
這兩個方法其實用于調用函數和類方法 、 類方法支持靜態。
然后 其他模塊里訪問:
~~~
$client = new \yar_client("http://nmp.cn/nmp_remote.php");
$ret = $client->function_call('build_part_commission_table', [['user_id'=>1, 'finish_time'=>'2017-10-28 10:00:00']]);
$data = [];
$ret2 = $client->class_call('\aa', 'sel', ['a']);
~~~