1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19:
20: class THttpClient extends TTransport {
21:
22: 23: 24: 25: 26:
27: protected $host_;
28:
29: 30: 31: 32: 33:
34: protected $port_;
35:
36: 37: 38: 39: 40:
41: protected $uri_;
42:
43: 44: 45: 46: 47:
48: protected $scheme_;
49:
50: 51: 52: 53: 54:
55: protected $buf_;
56:
57: 58: 59: 60: 61:
62: protected $handle_;
63:
64: 65: 66: 67: 68:
69: protected $timeout_;
70:
71: 72: 73: 74: 75: 76: 77:
78: public function __construct($host, $port=80, $uri='', $scheme = 'http') {
79: if ((strlen($uri) > 0) && ($uri{0} != '/')) {
80: $uri = '/'.$uri;
81: }
82: $this->scheme_ = $scheme;
83: $this->host_ = $host;
84: $this->port_ = $port;
85: $this->uri_ = $uri;
86: $this->buf_ = '';
87: $this->handle_ = null;
88: $this->timeout_ = null;
89: }
90:
91: 92: 93: 94: 95:
96: public function setTimeoutSecs($timeout) {
97: $this->timeout_ = $timeout;
98: }
99:
100: 101: 102: 103: 104:
105: public function isOpen() {
106: return true;
107: }
108:
109: 110: 111: 112: 113:
114: public function open() {}
115:
116: 117: 118:
119: public function close() {
120: if ($this->handle_) {
121: @fclose($this->handle_);
122: $this->handle_ = null;
123: }
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function read($len) {
134: $data = @fread($this->handle_, $len);
135: if ($data === FALSE || $data === '') {
136: $md = stream_get_meta_data($this->handle_);
137: if ($md['timed_out']) {
138: throw new TTransportException('THttpClient: timed out reading '.$len.' bytes from '.$this->host_.':'.$this->port_.'/'.$this->uri_, TTransportException::TIMED_OUT);
139: } else {
140: throw new TTransportException('THttpClient: Could not read '.$len.' bytes from '.$this->host_.':'.$this->port_.'/'.$this->uri_, TTransportException::UNKNOWN);
141: }
142: }
143: return $data;
144: }
145:
146: 147: 148: 149: 150: 151:
152: public function write($buf) {
153: $this->buf_ .= $buf;
154: }
155:
156: 157: 158: 159: 160:
161: public function flush() {
162:
163: $host = $this->host_.($this->port_ != 80 ? ':'.$this->port_ : '');
164:
165: $headers = array('Host: '.$host,
166: 'Accept: application/x-thrift',
167: 'User-Agent: PHP/THttpClient',
168: 'Content-Type: application/x-thrift',
169: 'Content-Length: '.strlen($this->buf_));
170:
171: $options = array('method' => 'POST',
172: 'header' => implode("\r\n", $headers),
173: 'max_redirects' => 1,
174: 'content' => $this->buf_);
175: if ($this->timeout_ > 0) {
176: $options['timeout'] = $this->timeout_;
177: }
178: $this->buf_ = '';
179:
180: $contextid = stream_context_create(array('http' => $options));
181: $this->handle_ = @fopen($this->scheme_.'://'.$host.$this->uri_, 'r', false, $contextid);
182:
183:
184: if ($this->handle_ === FALSE) {
185: $this->handle_ = null;
186: $error = 'THttpClient: Could not connect to '.$host.$this->uri_;
187: throw new TTransportException($error, TTransportException::NOT_OPEN);
188: }
189: }
190:
191: }
192:
193: ?>
194: