Client.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Thrift\Service;
  3. use Dever;
  4. use Thrift\Transport\TPhpStream;
  5. use Thrift\Protocol\TBinaryProtocol;
  6. use Thrift\Transport\TSocket;
  7. use Thrift\Transport\TBufferedTransport;
  8. use Thrift\Transport\THttpClient;
  9. use Thrift\Exception\TException;
  10. class Client extends Base
  11. {
  12. public function get($client, $callback, $param)
  13. {
  14. $socket = $this->{$this->type}();
  15. $transport = new TBufferedTransport($socket, 1024, 1024);
  16. $protocol = new TBinaryProtocol($transport);
  17. $client = new $client($protocol);
  18. $transport->open();
  19. array_unshift($param, $client);
  20. $result = call_user_func_array($callback, $param);
  21. //同步方式进行交互
  22. /*
  23. $recv = $client->sayHello('Courages');
  24. echo "\n sayHello11dd:".$recv." \n";
  25. //异步方式进行交互
  26. /*
  27. $client->send_sayHello('Us');
  28. echo "\n send_sayHello \n";
  29. $recv = $client->recv_sayHello();
  30. echo "\n recv_sayHello:".$recv." \n";
  31. */
  32. $transport->close();
  33. return $result;
  34. }
  35. private function socket()
  36. {
  37. return new TSocket($this->host, $this->port);
  38. }
  39. public function stream()
  40. {
  41. return new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W);
  42. }
  43. public function http()
  44. {
  45. return new THttpClient($this->host, $this->port, '/server.php');
  46. }
  47. }