198 * Constructor. |
205 * Constructor. |
199 * @param string Hostname to send to |
206 * @param string Hostname to send to |
200 * @param string URI (/index.php) |
207 * @param string URI (/index.php) |
201 * @param string Request method - GET or POST. |
208 * @param string Request method - GET or POST. |
202 * @param int Optional. The port to open the request on. Defaults to 80. |
209 * @param int Optional. The port to open the request on. Defaults to 80. |
203 */ |
210 * @param bool If true, uses SSL (and defaults the port to 443) |
204 |
211 */ |
205 function Request_HTTP($host, $uri, $method = 'GET', $port = 80) |
212 |
|
213 function Request_HTTP($host, $uri, $method = 'GET', $port = 'default', $ssl = false) |
206 { |
214 { |
207 if ( !preg_match('/^(?:(([a-z0-9-]+\.)*?)([a-z0-9-]+)|\[[a-f0-9:]+\])$/', $host) ) |
215 if ( !preg_match('/^(?:(([a-z0-9-]+\.)*?)([a-z0-9-]+)|\[[a-f0-9:]+\])$/', $host) ) |
208 throw new Exception(__CLASS__ . ': Invalid hostname'); |
216 throw new Exception(__CLASS__ . ': Invalid hostname'); |
|
217 if ( $ssl ) |
|
218 { |
|
219 $this->ssl = true; |
|
220 $port = ( $port === 'default' ) ? 443 : $port; |
|
221 } |
|
222 else |
|
223 { |
|
224 $this->ssl = false; |
|
225 $port = ( $port === 'default' ) ? 80 : $port; |
|
226 } |
209 // Yes - this really does support IPv6 URLs! |
227 // Yes - this really does support IPv6 URLs! |
210 $this->host = $host; |
228 $this->host = $host; |
211 $this->uri = $uri; |
229 $this->uri = $uri; |
212 if ( is_int($port) && $port >= 1 && $port <= 65535 ) |
230 if ( is_int($port) && $port >= 1 && $port <= 65535 ) |
213 $this->port = $port; |
231 $this->port = $port; |
331 */ |
349 */ |
332 |
350 |
333 function _sock_open(&$connection) |
351 function _sock_open(&$connection) |
334 { |
352 { |
335 // Open connection |
353 // Open connection |
336 $connection = fsockopen($this->host, $this->port, $errno, $errstr); |
354 $ssl_prepend = ( $this->ssl ) ? 'ssl://' : ''; |
|
355 $connection = fsockopen($ssl_prepend . $this->host, $this->port, $errno, $errstr); |
337 if ( !$connection ) |
356 if ( !$connection ) |
338 throw new Exception(__METHOD__ . ": Could not make connection"); // to {$this->host}:{$this->port}: error $errno: $errstr"); |
357 throw new Exception(__METHOD__ . ": Could not make connection"); // to {$this->host}:{$this->port}: error $errno: $errstr"); |
339 |
358 |
340 // 1 = socket open |
359 // 1 = socket open |
341 $this->state = 1; |
360 $this->state = 1; |