|
|
<?PHP
/** * struct * { * unisgned char type; * unsigned int time; * unsigned int serid; * unsigned int length; * char body[length]; * } */ function remoteCall(string $host, $port, string $data) { $length = strlen($data); $pack = pack('CNNNa*', 1, time(), 0, $length, $data); $client = new Swoole\Client(SWOOLE_SOCK_TCP);
$client->set(array( 'open_length_check' => true, 'package_max_length' => 81920, 'package_length_type' => 'N', 'package_length_offset' => 9, 'package_body_offset' => 13 ));
if (!$client->connect($host, $port, 0.5)) { return false; }
$client->send($pack); $pack = @$client->recv(); $client->close();
if ($pack === false) { //echo $client->errCode . "\n";
//TODO 获取错误码并进行处理
return false; } else if (empty($pack)) { echo "close by peer\n"; return false; }
$ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack); return $ret['data']; }
$ret = remoteCall("127.0.0.1", "5188", '{"controller":"site","methor":"test","params":{"id":1}}');
//'{"controller":"site","methor":"test","params":{"id":"111"}}
//'{"controller":"site","methor":"test","params":{"id":1,"status":0}}'
echo "{$ret}\n";
?>
|