Пример #1
0
 protected $url;
 private $filter;
 private $simple;
 protected abstract function send($request);
 public function __construct($url = '')
 {
     $this->useService($url);
     $this->filter = NULL;
     $this->simple = false;
 }
 public function useService($url = '', $namespace = '')
 {
     if ($url) {
         $this->url = $url;
     }
     return new HproseProxy($this, $namespace);
 }
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal, $simple = NULL)
 {
     if ($simple === NULL) {
         $simple = $this->simple;
     }
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = $simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream);
     $hproseWriter->writeString($functionName);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments);
         if ($byRef) {
             $hproseWriter->writeBoolean(true);
         }
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     if ($this->filter) {
         $request = $this->filter->outputFilter($request);
     }
     $stream->close();
     $response = $this->send($request);
     if ($this->filter) {
         $response = $this->filter->inputFilter($response);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
Пример #2
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (0 < count($arguments) || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
     }
     if ($byRef) {
         $hproseWriter->writeBoolean(true);
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     if ($this->filter) {
         $request = $this->filter->outputFilter($request);
     }
     $stream->close();
     $f = new SaeFetchurl();
     $cookie = $this->getCookie();
     if ($cookie != "") {
         $f->setHeader("Cookie", $cookie);
     }
     if ($this->keepAlive) {
         $f->setHeader("Connection", "keep-alive");
         $f->setHeader("Keep-Alive", $this->keepAliveTimeout);
     } else {
         $f->setHeader("Connection", "close");
     }
     foreach ($this->header as $name => $value) {
         $f->setHeader($name, $value);
     }
     $f->setMethod("post");
     $f->setPostData($request);
     $f->setConnectTimeout($this->timeout);
     $f->setSendTimeout($this->timeout);
     $f->setReadTimeout($this->timeout);
     $response = $f->fetch($this->url);
     if ($f->errno()) {
         throw new HproseException($f->errno() . ": " . $f->errmsg());
     }
     $http_response_header = $f->responseHeaders(false);
     $this->setCookie($http_response_header);
     if ($this->filter) {
         $response = $this->filter->inputFilter($response);
     }
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }
Пример #3
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
         if ($byRef) {
             $hproseWriter->writeBoolean(true);
         }
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     $stream->close();
     $opts = array('http' => array('method' => 'POST', 'header' => $this->getCookie() . "Content-Length: " . strlen($request) . "\r\n" . ($this->keepAlive ? "Connection: keep-alive\r\n" . "Keep-Alive: " . $this->keepAliveTimeout . "\r\n" : "Connection: close\r\n"), 'content' => $request, 'timeout' => $this->timeout / 1000.0));
     foreach ($this->header as $name => $value) {
         $opts['http']['header'] .= "{$name}: {$value}\r\n";
     }
     if ($this->proxy) {
         $opts['http']['proxy'] = $this->proxy;
         $opts['http']['request_fulluri'] = true;
     }
     $context = stream_context_create($opts);
     set_error_handler(array(&$this, '__errorHandler'));
     $response = file_get_contents($this->url, false, $context);
     restore_error_handler();
     $this->setCookie($http_response_header);
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags::TagArgument:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }
Пример #4
0
 public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal)
 {
     $stream = new HproseStringStream(HproseTags::TagCall);
     $hproseWriter = new HproseWriter($stream);
     $hproseWriter->writeString($functionName, false);
     if (count($arguments) > 0 || $byRef) {
         $hproseWriter->reset();
         $hproseWriter->writeList($arguments, false);
     }
     if ($byRef) {
         $hproseWriter->writeBoolean(true);
     }
     $stream->write(HproseTags::TagEnd);
     $request = $stream->toString();
     $stream->close();
     curl_setopt($this->curl, CURLOPT_URL, $this->url);
     curl_setopt($this->curl, CURLOPT_HEADER, TRUE);
     curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($this->curl, CURLOPT_POST, TRUE);
     curl_setopt($this->curl, CURLOPT_POSTFIELDS, $request);
     $headers_array = array($this->getCookie(), "Content-Length: " . strlen($request));
     if ($this->keepAlive) {
         $headers_array[] = "Connection: keep-alive";
         $headers_array[] = "Keep-Alive: " . $this->keepAliveTimeout;
     } else {
         $headers_array[] = "Connection: close";
     }
     foreach ($this->header as $name => $value) {
         $headers_array[] = $name . ": " . $value;
     }
     curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers_array);
     if ($this->proxy) {
         curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy);
     }
     if (defined(CURLOPT_TIMEOUT_MS)) {
         curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, $this->timeout);
     } else {
         curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout / 1000);
     }
     $response = curl_exec($this->curl);
     $errno = curl_errno($this->curl);
     if ($errno) {
         throw new HproseException($errno . ": " . curl_error($this->curl));
     }
     do {
         list($response_headers, $response) = explode("\r\n\r\n", $response, 2);
         $http_response_header = explode("\r\n", $response_headers);
         $http_response_firstline = array_shift($http_response_header);
         if (preg_match('@^HTTP/[0-9]\\.[0-9]\\s([0-9]{3})\\s(.*?)@', $http_response_firstline, $matches)) {
             $response_code = $matches[1];
             $response_status = trim($matches[2]);
         } else {
             $response_code = "500";
             $response_status = "Unknown Error.";
         }
     } while (substr($response_code, 0, 1) == "1");
     if ($response_code != '200') {
         throw new HproseException($response_code . ": " . $response_status);
     }
     $this->setCookie($http_response_header);
     if ($resultMode == HproseResultMode::RawWithEndTag) {
         return $response;
     }
     if ($resultMode == HproseResultMode::Raw) {
         return substr($response, 0, -1);
     }
     $stream = new HproseStringStream($response);
     $hproseReader = new HproseReader($stream);
     $result = NULL;
     $error = NULL;
     while (($tag = $hproseReader->checkTags(array(HproseTags::TagResult, HproseTags::TagArgument, HproseTags::TagError, HproseTags::TagEnd))) !== HproseTags::TagEnd) {
         switch ($tag) {
             case HproseTags::TagResult:
                 if ($resultMode == HproseResultMode::Serialized) {
                     $result = $hproseReader->readRaw()->toString();
                 } else {
                     $hproseReader->reset();
                     $result =& $hproseReader->unserialize();
                 }
                 break;
             case HproseTags::TagArgument:
                 $hproseReader->reset();
                 $args =& $hproseReader->readList();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $arguments[$i] =& $args[$i];
                 }
                 break;
             case HproseTags::TagError:
                 $hproseReader->reset();
                 $error = new HproseException($hproseReader->readString());
                 break;
         }
     }
     if (!is_null($error)) {
         throw $error;
     }
     return $result;
 }