4 * Copyright (c) 2006- Facebook
5 * Distributed under the Thrift Software License
7 * See accompanying file LICENSE or visit the Thrift site at:
8 * http://developers.facebook.com/thrift/
10 * @package thrift.transport
11 * @author Mark Slee <mcslee@facebook.com>
15 * HTTP client for Thrift
17 * @package thrift.transport
18 * @author Mark Slee <mcslee@facebook.com>
20 class THttpClient
extends TTransport
{
23 * The host to connect to
30 * The port to connect on
44 * Buffer for the HTTP request data
51 * Input socket stream.
65 * Make a new HTTP client.
71 public function __construct($host, $port=80, $uri='') {
72 if ((strlen($uri) > 0) && ($uri{0} != '/')) {
79 $this->handle_
= null;
80 $this->timeout_
= null;
86 * @param float $timeout
88 public function setTimeoutSecs($timeout) {
89 $this->timeout_
= $timeout;
93 * Whether this transport is open.
95 * @return boolean true if open
97 public function isOpen() {
102 * Open the transport for reading/writing
104 * @throws TTransportException if cannot open
106 public function open() {}
109 * Close the transport.
111 public function close() {
112 if ($this->handle_
) {
113 @fclose
($this->handle_
);
114 $this->handle_
= null;
119 * Read some data into the array.
121 * @param int $len How much to read
122 * @return string The data that has been read
123 * @throws TTransportException if cannot read any more data
125 public function read($len) {
126 $data = @fread
($this->handle_
, $len);
127 if ($data === FALSE ||
$data === '') {
128 $md = stream_get_meta_data($this->handle_
);
129 if ($md['timed_out']) {
130 throw new TTransportException('THttpClient: timed out reading '.$len.' bytes from '.$this->host_
.':'.$this->port_
.'/'.$this->uri_
, TTransportException
::TIMED_OUT
);
132 throw new TTransportException('THttpClient: Could not read '.$len.' bytes from '.$this->host_
.':'.$this->port_
.'/'.$this->uri_
, TTransportException
::UNKNOWN
);
139 * Writes some data into the pending buffer
141 * @param string $buf The data to write
142 * @throws TTransportException if writing fails
144 public function write($buf) {
149 * Opens and sends the actual request over the HTTP connection
151 * @throws TTransportException if a writing error occurs
153 public function flush() {
154 // God, PHP really has some esoteric ways of doing simple things.
155 $host = $this->host_
.($this->port_
!= 80 ?
':'.$this->port_
: '');
157 $headers = array('Host: '.$host,
158 'Accept: application/x-thrift',
159 'User-Agent: PHP/THttpClient',
160 'Content-Type: application/x-thrift',
161 'Content-Length: '.strlen($this->buf_
));
163 $options = array('method' => 'POST',
164 'header' => implode("\r\n", $headers),
165 'max_redirects' => 1,
166 'content' => $this->buf_
);
167 if ($this->timeout_
> 0) {
168 $options['timeout'] = $this->timeout_
;
172 $contextid = stream_context_create(array('http' => $options));
173 $this->handle_
= @fopen
('http://'.$host.$this->uri_
, 'r', false, $contextid);
176 if ($this->handle_
=== FALSE) {
177 $this->handle_
= null;
178 $error = 'THttpClient: Could not connect to '.$host.$this->uri_
;
179 throw new TTransportException($error, TTransportException
::NOT_OPEN
);