46 $this->m_host = $host;
47 $this->m_port = $port;
48 $this->m_scheme = $scheme;
49 $this->m_headers = array();
60 $this->m_headers[$key] = $value;
71 function send($target =
'/', $method =
'GET', $timeout = 3, $post_vars = NULL)
73 static $allow_methods = NULL;
78 $method = strtoupper($method);
81 $allow_methods = explode(
' ',
'GET POST PUT');
83 if(!in_array($method, $allow_methods))
85 $method = $allow_methods[0];
89 $timout = max((
int) $timeout, 0);
92 if(!is_array($post_vars))
97 if(FALSE && is_callable(
'curl_init'))
99 return $this->
sendWithCurl($target, $method, $timeout, $post_vars);
103 return $this->
sendWithSock($target, $method, $timeout, $post_vars);
117 static $crlf =
"\r\n";
120 if($this->m_scheme==
'https')
125 $sock = @fsockopen($scheme . $this->m_host, $this->m_port, $errno, $errstr, $timeout);
128 return new BaseObject(-1,
'socket_connect_failed');
131 $headers = $this->m_headers + array();
132 if(!isset($headers[
'Accept-Encoding']))
134 $headers[
'Accept-Encoding'] =
'identity';
139 if($method ==
'POST' && count($post_vars))
141 foreach($post_vars as $key => $value)
143 $post_body .= urlencode($key) .
'=' . urlencode($value) .
'&';
145 $post_body = substr($post_body, 0, -1);
147 $headers[
'Content-Length'] = strlen($post_body);
148 $headers[
'Content-Type'] =
'application/x-www-form-urlencoded';
151 $request =
"$method $target HTTP/1.1$crlf";
152 foreach($headers as $equiv => $content)
154 $request .=
"$equiv: $content$crlf";
156 $request .= $crlf . $post_body;
157 fwrite($sock, $request);
159 list($httpver, $code, $status) = preg_split(
'/ +/', rtrim(fgets($sock)), 3);
163 while(strlen(trim($line = fgets($sock))))
165 list($equiv, $content) = preg_split(
'/ *: */', rtrim($line), 2);
166 if(!strcasecmp($equiv,
'Transfer-Encoding') && $content ==
'chunked')
177 $chunk_size = hexdec(fgets($sock));
180 $body .= fgets($sock, $chunk_size+1);
185 $body .= fgets($sock, 512);
191 $ret->result_code = $code;
207 $headers = $this->m_headers + array();
212 $headers[
'Expect'] =
'';
215 curl_setopt($ch, CURLOPT_URL,
"http://{$this->m_host}{$target}");
216 curl_setopt($ch, CURLOPT_HEADER, FALSE);
217 curl_setopt($ch, CURLOPT_PORT, $this->m_port);
218 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
219 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
220 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
224 case 'GET': curl_setopt($ch, CURLOPT_HTTPGET,
true);
226 case 'PUT': curl_setopt($ch, CURLOPT_PUT,
true);
229 curl_setopt($ch, CURLOPT_POST,
true);
230 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
234 $arr_headers = array();
235 foreach($headers as $key => $value)
237 $arr_headers[] =
"$key: $value";
240 curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_headers);
242 $body = curl_exec($ch);
245 return new BaseObject(-1,
'socket_connect_failed');
248 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
251 $ret->result_code = $code;
send($target= '/', $method= 'GET', $timeout=3, $post_vars=NULL)
sendWithCurl($target, $method, $timeout, $post_vars)
addToHeader($key, $value)
sendWithSock($target, $method, $timeout, $post_vars)
__construct($host, $port, $scheme='')