XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
XEHttpRequest.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
14 {
15 
20  var $m_host;
21 
26  var $m_port;
27 
32  var $m_scheme;
33 
39 
44  function __construct($host, $port, $scheme='')
45  {
46  $this->m_host = $host;
47  $this->m_port = $port;
48  $this->m_scheme = $scheme;
49  $this->m_headers = array();
50  }
51 
58  function addToHeader($key, $value)
59  {
60  $this->m_headers[$key] = $value;
61  }
62 
71  function send($target = '/', $method = 'GET', $timeout = 3, $post_vars = NULL)
72  {
73  static $allow_methods = NULL;
74 
75  $this->addToHeader('Host', $this->m_host);
76  $this->addToHeader('Connection', 'close');
77 
78  $method = strtoupper($method);
79  if(!$allow_methods)
80  {
81  $allow_methods = explode(' ', 'GET POST PUT');
82  }
83  if(!in_array($method, $allow_methods))
84  {
85  $method = $allow_methods[0];
86  }
87 
88  // $timeout should be an integer that is bigger than zero
89  $timout = max((int) $timeout, 0);
90 
91  // list of post variables
92  if(!is_array($post_vars))
93  {
94  $post_vars = array();
95  }
96 
97  if(FALSE && is_callable('curl_init'))
98  {
99  return $this->sendWithCurl($target, $method, $timeout, $post_vars);
100  }
101  else
102  {
103  return $this->sendWithSock($target, $method, $timeout, $post_vars);
104  }
105  }
106 
115  function sendWithSock($target, $method, $timeout, $post_vars)
116  {
117  static $crlf = "\r\n";
118 
119  $scheme = '';
120  if($this->m_scheme=='https')
121  {
122  $scheme = 'ssl://';
123  }
124 
125  $sock = @fsockopen($scheme . $this->m_host, $this->m_port, $errno, $errstr, $timeout);
126  if(!$sock)
127  {
128  return new BaseObject(-1, 'socket_connect_failed');
129  }
130 
131  $headers = $this->m_headers + array();
132  if(!isset($headers['Accept-Encoding']))
133  {
134  $headers['Accept-Encoding'] = 'identity';
135  }
136 
137  // post body
138  $post_body = '';
139  if($method == 'POST' && count($post_vars))
140  {
141  foreach($post_vars as $key => $value)
142  {
143  $post_body .= urlencode($key) . '=' . urlencode($value) . '&';
144  }
145  $post_body = substr($post_body, 0, -1);
146 
147  $headers['Content-Length'] = strlen($post_body);
148  $headers['Content-Type'] = 'application/x-www-form-urlencoded';
149  }
150 
151  $request = "$method $target HTTP/1.1$crlf";
152  foreach($headers as $equiv => $content)
153  {
154  $request .= "$equiv: $content$crlf";
155  }
156  $request .= $crlf . $post_body;
157  fwrite($sock, $request);
158 
159  list($httpver, $code, $status) = preg_split('/ +/', rtrim(fgets($sock)), 3);
160 
161  // read response headers
162  $is_chunked = FALSE;
163  while(strlen(trim($line = fgets($sock))))
164  {
165  list($equiv, $content) = preg_split('/ *: */', rtrim($line), 2);
166  if(!strcasecmp($equiv, 'Transfer-Encoding') && $content == 'chunked')
167  {
168  $is_chunked = TRUE;
169  }
170  }
171 
172  $body = '';
173  while(!feof($sock))
174  {
175  if($is_chunked)
176  {
177  $chunk_size = hexdec(fgets($sock));
178  if($chunk_size)
179  {
180  $body .= fgets($sock, $chunk_size+1);
181  }
182  }
183  else
184  {
185  $body .= fgets($sock, 512);
186  }
187  }
188  fclose($sock);
189 
190  $ret = new stdClass;
191  $ret->result_code = $code;
192  $ret->body = $body;
193 
194  return $ret;
195  }
196 
205  function sendWithCurl($target, $method, $timeout, $post_vars)
206  {
207  $headers = $this->m_headers + array();
208 
209  // creat a new cURL resource
210  $ch = curl_init();
211 
212  $headers['Expect'] = '';
213 
214  // set URL and other appropriate options
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);
221 
222  switch($method)
223  {
224  case 'GET': curl_setopt($ch, CURLOPT_HTTPGET, true);
225  break;
226  case 'PUT': curl_setopt($ch, CURLOPT_PUT, true);
227  break;
228  case 'POST':
229  curl_setopt($ch, CURLOPT_POST, true);
230  curl_setopt($ch, CURLOPT_POSTFIELDS, $post_vars);
231  break;
232  }
233 
234  $arr_headers = array();
235  foreach($headers as $key => $value)
236  {
237  $arr_headers[] = "$key: $value";
238  }
239 
240  curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_headers);
241 
242  $body = curl_exec($ch);
243  if(curl_errno($ch))
244  {
245  return new BaseObject(-1, 'socket_connect_failed');
246  }
247 
248  $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
249 
250  $ret = new stdClass;
251  $ret->result_code = $code;
252  $ret->body = $body;
253 
254  return $ret;
255  }
256 
257 }
258 /* End of file XEHttpRequest.class.php */
259 /* Location: ./classes/httprequest/XEHttpRequest.class.php */
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='')