Commit cbef3fb7 authored by 杨培's avatar 杨培

hyperf jsonrpc 调用服务

parent e1e305a4
...@@ -18,11 +18,16 @@ Member::setDebug(); ...@@ -18,11 +18,16 @@ Member::setDebug();
//Member::updateAvatar(12, 'jpg', base64_encode($bin)); //Member::updateAvatar(12, 'jpg', base64_encode($bin));
//$ret = Member::getWechatInfoFromCode('dasfasdfsdaf'); //$ret = Member::getWechatInfoFromCode('dasfasdfsdaf');
$ret = Member::getMemberInfo(793, ['basic', 'vip', 'verify']); //$ret = Member::getMemberInfo(793, ['basic', 'vip', 'verify']);
print_r($ret); //print_r($ret);
//$info = Member::getMemberInfo(12); //$info = Member::getMemberInfo(12);
// $info = Member::getInfoByMobile('15395081586'); // $info = Member::getInfoByMobile('15395081586');
//$info = Member::getSyncByMobile('13000000002'); //$info = Member::getSyncByMobile('13000000002');
//$info = Member::getSyncByUid(514); //$info = Member::getSyncByUid(514);
//print_r($info); //print_r($info);
//$rt = Member::invol('hello world, blabl blasdfjiasdpjf -saduf0 asdupf0ashd- sajdgm 0df9n2i-df0m9gp-dfs0kgi-90skmjikg-0jsk-fkgdfg');
$rt = Member::retrive();
print_r($rt);
...@@ -26,6 +26,7 @@ class Client ...@@ -26,6 +26,7 @@ class Client
protected $clientSecret; protected $clientSecret;
protected $apiUrl; protected $apiUrl;
protected $remoteIp; protected $remoteIp;
protected $rpcServer;
protected static $instance = []; protected static $instance = [];
protected static $config = []; protected static $config = [];
...@@ -201,6 +202,25 @@ class Client ...@@ -201,6 +202,25 @@ class Client
return $client->put(1024, 0, 60, json_encode($msg, JSON_UNESCAPED_UNICODE)); return $client->put(1024, 0, 60, json_encode($msg, JSON_UNESCAPED_UNICODE));
} }
protected static function invoke($method, $params=[], $id=null)
{
static $connections = [];
$instance = self::getInstance();
if (!isset($connections[$instance->rpcServer])) {
$srv = parse_url($instance->rpcServer);
$conn = new HyperfJsonRpc([
'host' => $srv['host'],
'port' => $srv['port']
]);
$conn->connect();
$connections[$instance->rpcServer] = $conn;
}
return $connections[$instance->rpcServer]->invoke($method, $params, $id);
}
/** /**
* 对请求进程签名 * 对请求进程签名
* *
......
<?php
/**
* Created by PhpStorm.
* User: pader
* Date: 2019/12/4
* Time: 17:26
*/
namespace internalApi\base;
use RuntimeException;
class HyperfJsonRpc
{
/**
* Holds a boolean indicating whether a connection to the server is
* currently established or not.
*
* @var boolean
*/
public $connected = false;
/**
* Holds configuration values.
*
* @var array
*/
protected $_config = [];
/**
* The current connection resource handle (if any).
*
* @var resource
*/
protected $_connection;
public function __construct(array $config = []) {
$defaults = [
'persistent' => true,
'host' => '127.0.0.1',
'port' => 11300,
'timeout' => 1,
'logger' => null
];
$this->_config = $config + $defaults;
}
/**
* Destructor, disconnects from the server.
*
* @return void
*/
public function __destruct() {
$this->disconnect();
}
/**
* Initiates a socket connection to the beanstalk server. The resulting
* stream will not have any timeout set on it. Which means it can wait
* an unlimited amount of time until a packet becomes available. This
* is required for doing blocking reads.
*
* @see \internalApi\base\Beanstalk::$_connection
* @see \internalApi\base\Beanstalk::reserve()
* @return boolean `true` if the connection was established, `false` otherwise.
*/
public function connect() {
if (isset($this->_connection)) {
$this->disconnect();
}
$function = $this->_config['persistent'] ? 'pfsockopen' : 'fsockopen';
$params = [$this->_config['host'], $this->_config['port'], &$errNum, &$errStr];
if ($this->_config['timeout']) {
$params[] = $this->_config['timeout'];
}
$this->_connection = @call_user_func_array($function, $params);
if (!empty($errNum) || !empty($errStr)) {
$this->_error("{$errNum}: {$errStr}");
}
$this->connected = is_resource($this->_connection);
if ($this->connected) {
stream_set_timeout($this->_connection, -1);
}
return $this->connected;
}
/**
* Closes the connection to the beanstalk server by first signaling
* that we want to quit then actually closing the socket connection.
*
* @return boolean `true` if diconnecting was successful.
*/
public function disconnect() {
if (!is_resource($this->_connection)) {
$this->connected = false;
} else {
$this->connected = !fclose($this->_connection);
if (!$this->connected) {
$this->_connection = null;
}
}
return !$this->connected;
}
/**
* Pushes an error message to the logger, when one is configured.
*
* @param string $message The error message.
* @return void
*/
protected function _error($message) {
if ($this->_config['logger']) {
$this->_config['logger']->error($message);
}
}
/**
* Writes a packet to the socket. Prior to writing to the socket will
* check for availability of the connection.
*
* @param string $data
* @return integer|boolean number of written bytes or `false` on error.
*/
protected function _write($data) {
if (!$this->connected) {
$message = 'No connecting found while writing data to socket.';
throw new RuntimeException($message);
}
$data .= "\r\n";
return fwrite($this->_connection, $data, strlen($data));
}
/**
* Reads a packet from the socket. Prior to reading from the socket
* will check for availability of the connection.
*
* @param integer $length Number of bytes to read.
* @return string|boolean Data or `false` on error.
*/
protected function _read() {
if (!$this->connected) {
$message = 'No connection found while reading data from socket.';
throw new RuntimeException($message);
}
return stream_get_line($this->_connection, 16384, "\r\n");
}
public function invoke($method, $params, $id=null)
{
$data = [
'jsonrpc' => '2.0',
'method' => $method,
'params' => $params,
'id' => $id
];
$json = json_encode($data);
$this->_write($json);
$response = $this->_read();
if (!$response) {
return false;
}
$ret = json_decode($response, true);
if (!empty($ret['error'])) {
$this->_error($ret['error']['message']);
return false;
}
return $ret['result'];
}
}
\ No newline at end of file
...@@ -10,7 +10,8 @@ return [ ...@@ -10,7 +10,8 @@ return [
'ucenter' => [ 'ucenter' => [
'apiUrl' => 'http://u.yunya.com/api/server', 'apiUrl' => 'http://u.yunya.com/api/server',
'clientId' => 'common', 'clientId' => 'common',
'clientSecret' => 'Kuy2dVHKRWKAFRXX2l6k' 'clientSecret' => 'Kuy2dVHKRWKAFRXX2l6k',
'rpcServer' => '192.168.99.181:9503'
], ],
'xue' => [ 'xue' => [
'apiUrl' => 'http://xue.yunya.com/api', 'apiUrl' => 'http://xue.yunya.com/api',
......
...@@ -201,4 +201,14 @@ class Member extends Client ...@@ -201,4 +201,14 @@ class Member extends Client
return self::call('member.getUidByWxhuid', compact('wxhuid')); return self::call('member.getUidByWxhuid', compact('wxhuid'));
} }
public static function invol($say)
{
return self::invoke('/test/invoid', [$say]);
}
public static function retrive()
{
return self::invoke('/test/retrive');
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment