You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?PHP
  2. /**
  3. * struct
  4. * {
  5. * unisgned char type;
  6. * unsigned int time;
  7. * unsigned int serid;
  8. * unsigned int length;
  9. * char body[length];
  10. * }
  11. */
  12. function remoteCall(string $host, $port, string $data) {
  13. $length = strlen($data);
  14. $pack = pack('CNNNa*', 1, time(), 0, $length, $data);
  15. $client = new Swoole\Client(SWOOLE_SOCK_TCP);
  16. $client->set(array(
  17. 'open_length_check' => true,
  18. 'package_max_length' => 81920,
  19. 'package_length_type' => 'N',
  20. 'package_length_offset' => 9,
  21. 'package_body_offset' => 13
  22. ));
  23. if (!$client->connect($host, $port, 0.5)) {
  24. return false;
  25. }
  26. $client->send($pack);
  27. $pack = @$client->recv();
  28. $client->close();
  29. if ($pack === false) {
  30. //echo $client->errCode . "\n";
  31. //TODO 获取错误码并进行处理
  32. return false;
  33. } else if (empty($pack)) {
  34. echo "close by peer\n";
  35. return false;
  36. }
  37. $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack);
  38. return $ret['data'];
  39. }
  40. $ret = remoteCall("127.0.0.1", "5188", '{"controller":"site","methor":"test","params":{"id":1}}');
  41. //'{"controller":"site","methor":"test","params":{"id":"111"}}
  42. //'{"controller":"site","methor":"test","params":{"id":1,"status":0}}'
  43. echo "{$ret}\n";
  44. ?>