HproseClient.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**********************************************************\
  3. | |
  4. | hprose |
  5. | |
  6. | Official WebSite: http://www.hprose.com/ |
  7. | http://www.hprose.net/ |
  8. | http://www.hprose.org/ |
  9. | |
  10. \**********************************************************/
  11. /**********************************************************\
  12. * *
  13. * HproseClient.php *
  14. * *
  15. * hprose client library for php5. *
  16. * *
  17. * LastModified: Nov 13, 2013 *
  18. * Author: Ma Bingyao <andot@hprfc.com> *
  19. * *
  20. \**********************************************************/
  21. require_once('HproseCommon.php');
  22. require_once('HproseIO.php');
  23. abstract class HproseClient {
  24. protected $url;
  25. private $filter;
  26. private $simple;
  27. protected abstract function send($request);
  28. public function __construct($url = '') {
  29. $this->useService($url);
  30. $this->filter = NULL;
  31. $this->simple = false;
  32. }
  33. public function useService($url = '', $namespace = '') {
  34. if ($url) {
  35. $this->url = $url;
  36. }
  37. return new HproseProxy($this, $namespace);
  38. }
  39. public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal, $simple = NULL) {
  40. if ($simple === NULL) $simple = $this->simple;
  41. $stream = new HproseStringStream(HproseTags::TagCall);
  42. $hproseWriter = ($simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream));
  43. $hproseWriter->writeString($functionName);
  44. if (count($arguments) > 0 || $byRef) {
  45. $hproseWriter->reset();
  46. $hproseWriter->writeList($arguments);
  47. if ($byRef) {
  48. $hproseWriter->writeBoolean(true);
  49. }
  50. }
  51. $stream->write(HproseTags::TagEnd);
  52. $request = $stream->toString();
  53. if ($this->filter) $request = $this->filter->outputFilter($request);
  54. $stream->close();
  55. $response = $this->send($request);
  56. if ($this->filter) $response = $this->filter->inputFilter($response);
  57. if ($resultMode == HproseResultMode::RawWithEndTag) {
  58. return $response;
  59. }
  60. if ($resultMode == HproseResultMode::Raw) {
  61. return substr($response, 0, -1);
  62. }
  63. $stream = new HproseStringStream($response);
  64. $hproseReader = new HproseReader($stream);
  65. $result = NULL;
  66. while (($tag = $hproseReader->checkTags(
  67. array(HproseTags::TagResult,
  68. HproseTags::TagArgument,
  69. HproseTags::TagError,
  70. HproseTags::TagEnd))) !== HproseTags::TagEnd) {
  71. switch ($tag) {
  72. case HproseTags::TagResult:
  73. if ($resultMode == HproseResultMode::Serialized) {
  74. $result = $hproseReader->readRaw()->toString();
  75. }
  76. else {
  77. $hproseReader->reset();
  78. $result = &$hproseReader->unserialize();
  79. }
  80. break;
  81. case HproseTags::TagArgument:
  82. $hproseReader->reset();
  83. $args = &$hproseReader->readList(true);
  84. for ($i = 0; $i < count($arguments); $i++) {
  85. $arguments[$i] = &$args[$i];
  86. }
  87. break;
  88. case HproseTags::TagError:
  89. $hproseReader->reset();
  90. throw new HproseException($hproseReader->readString(true));
  91. break;
  92. }
  93. }
  94. return $result;
  95. }
  96. public function getFilter() {
  97. return $this->filter;
  98. }
  99. public function setFilter($filter) {
  100. $this->filter = $filter;
  101. }
  102. public function getSimpleMode() {
  103. return $this->simple;
  104. }
  105. public function setSimpleMode($simple = true) {
  106. $this->simple = $simple;
  107. }
  108. public function __call($function, $arguments) {
  109. return $this->invoke($function, $arguments);
  110. }
  111. public function __get($name) {
  112. return new HproseProxy($this, $name . '_');
  113. }
  114. }
  115. class HproseProxy {
  116. private $client;
  117. private $namespace;
  118. public function __construct($client, $namespace = '') {
  119. $this->client = $client;
  120. $this->namespace = $namespace;
  121. }
  122. public function __call($function, $arguments) {
  123. $function = $this->namespace . $function;
  124. return $this->client->invoke($function, $arguments);
  125. }
  126. public function __get($name) {
  127. return new HproseProxy($this->client, $this->namespace . $name . '_');
  128. }
  129. }
  130. ?>