/**
  * Guarantees that the full amount of data is read.
  *
  * @return string The data, of exact length
  * @throws TTransportException if cannot read data
  */
 public function readAll($len)
 {
     // return $this->read($len);
     $data = '';
     $got = 0;
     while (($got = TStringFuncFactory::create()->strlen($data)) < $len) {
         $data .= $this->read($len - $got);
     }
     return $data;
 }
 public function writeString($value)
 {
     $len = TStringFuncFactory::create()->strlen($value);
     $result = $this->writeI32($len);
     if ($len) {
         $this->trans_->write($value, $len);
     }
     return $result + $len;
 }
 public function flush()
 {
     if (TStringFuncFactory::create()->strlen($this->wBuf_) > 0) {
         $out = $this->wBuf_;
         // Note that we clear the internal wBuf_ prior to the underlying write
         // to ensure we're in a sane state (i.e. internal buffer cleaned)
         // if the underlying write throws up an exception
         $this->wBuf_ = '';
         $this->transport_->write($out);
     }
     $this->transport_->flush();
 }
 public function available()
 {
     return TStringFuncFactory::create()->strlen($this->buf_);
 }
 /**
  * Opens and sends the actual request over the HTTP connection
  *
  * @throws TTransportException if a writing error occurs
  */
 public function flush()
 {
     if (!self::$curlHandle) {
         register_shutdown_function(array('Thrift\\Transport\\TCurlClient', 'closeCurlHandle'));
         self::$curlHandle = curl_init();
         curl_setopt(self::$curlHandle, CURLOPT_RETURNTRANSFER, true);
         curl_setopt(self::$curlHandle, CURLOPT_BINARYTRANSFER, true);
         curl_setopt(self::$curlHandle, CURLOPT_USERAGENT, 'PHP/TCurlClient');
         curl_setopt(self::$curlHandle, CURLOPT_CUSTOMREQUEST, 'POST');
         curl_setopt(self::$curlHandle, CURLOPT_FOLLOWLOCATION, true);
         curl_setopt(self::$curlHandle, CURLOPT_MAXREDIRS, 1);
     }
     // God, PHP really has some esoteric ways of doing simple things.
     $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : '');
     $fullUrl = $this->scheme_ . "://" . $host . $this->uri_;
     $headers = array('Accept: application/x-thrift', 'Content-Type: application/x-thrift', 'Content-Length: ' . TStringFuncFactory::create()->strlen($this->request_));
     curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers);
     if ($this->timeout_ > 0) {
         curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_);
     }
     curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_);
     $this->request_ = '';
     curl_setopt(self::$curlHandle, CURLOPT_URL, $fullUrl);
     $this->response_ = curl_exec(self::$curlHandle);
     // Connect failed?
     if (!$this->response_) {
         curl_close(self::$curlHandle);
         self::$curlHandle = null;
         $error = 'TCurlClient: Could not connect to ' . $fullUrl;
         throw new TTransportException($error, TTransportException::NOT_OPEN);
     }
 }
 /**
  * Opens and sends the actual request over the HTTP connection
  *
  * @throws TTransportException if a writing error occurs
  */
 public function flush()
 {
     // God, PHP really has some esoteric ways of doing simple things.
     $host = $this->host_ . ($this->port_ != 80 ? ':' . $this->port_ : '');
     $headers = array();
     $defaultHeaders = array('Host' => $host, 'Accept' => 'application/x-thrift', 'User-Agent' => 'PHP/THttpClient', 'Content-Type' => 'application/x-thrift', 'Content-Length' => TStringFuncFactory::create()->strlen($this->buf_));
     foreach (array_merge($defaultHeaders, $this->headers_) as $key => $value) {
         $headers[] = "{$key}: {$value}";
     }
     $options = array('method' => 'POST', 'header' => implode("\r\n", $headers), 'max_redirects' => 1, 'content' => $this->buf_);
     if ($this->timeout_ > 0) {
         $options['timeout'] = $this->timeout_;
     }
     $this->buf_ = '';
     $contextid = stream_context_create(array('http' => $options));
     $this->handle_ = @fopen($this->scheme_ . '://' . $host . $this->uri_, 'r', false, $contextid);
     // Connect failed?
     if ($this->handle_ === FALSE) {
         $this->handle_ = null;
         $error = 'THttpClient: Could not connect to ' . $host . $this->uri_;
         throw new TTransportException($error, TTransportException::NOT_OPEN);
     }
 }
Beispiel #7
0
 /**
  * Write to the socket.
  *
  * @param string $buf The data to write
  */
 public function write($buf)
 {
     $null = null;
     $write = array($this->handle_);
     // keep writing until all the data has been written
     while (TStringFuncFactory::create()->strlen($buf) > 0) {
         // wait for stream to become available for writing
         $writable = @stream_select($null, $write, $null, $this->sendTimeoutSec_, $this->sendTimeoutUsec_);
         if ($writable > 0) {
             // write buffer to stream
             $written = @stream_socket_sendto($this->handle_, $buf);
             if ($written === -1 || $written === false) {
                 throw new TTransportException('TSocket: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes ' . $this->host_ . ':' . $this->port_);
             }
             // determine how much of the buffer is left to write
             $buf = TStringFuncFactory::create()->substr($buf, $written);
         } elseif ($writable === 0) {
             throw new TTransportException('TSocket: timed out writing ' . TStringFuncFactory::create()->strlen($buf) . ' bytes from ' . $this->host_ . ':' . $this->port_);
         } else {
             throw new TTransportException('TSocket: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes ' . $this->host_ . ':' . $this->port_);
         }
     }
 }
 public function write($buf)
 {
     while (TStringFuncFactory::create()->strlen($buf) > 0) {
         $got = @fwrite($this->outStream_, $buf);
         if ($got === 0 || $got === FALSE) {
             throw new TException('TPhpStream: Could not write ' . TStringFuncFactory::create()->strlen($buf) . ' bytes');
         }
         $buf = TStringFuncFactory::create()->substr($buf, $got);
     }
 }