Class HTTP_Connection

Description

HTTP Connection class

Instance of this class represents a connection to a HTTP server.

This class is experimental.

Example:


1 $http =& new HTTP_Connection('stopwar.org.uk', 80, 10, 30, array('max_header_lines'=>50));
2 // 1st argument: host
3 // 2nd argument: optioanl - port (default: 80)
4 // 3rd argument: optional - connection timeout
5 // 4th argument: optional - read/write timeout
6 // 5th argument: optional - options (at the moment, it's only 'max_header_lines' and
7

See test_HTTP_Connection.php for more examples

TODO:

  • read_trailers() method

Located in Program_Root/HTTP_Connection.php (line 51)


	
			
Variable Summary
Method Summary
void HTTP_Connection (mixed $host, [mixed $port = 80], [mixed $timeout_connection = 10], [mixed $options = null], int $status_code, string $reason_phrase, object $headers, string $body)
bool clear_error ()
bool configure (array $options)
bool disconnect ()
bool eof ()
string format_chunk (mixed $string)
string get_error_string ()
bool has_error ()
string read (int $length)
mixed read_chunk ()
array read_headers ()
string read_line ([mixed $length = 1024])
mixed read_status_line ()
bool set_timeout (int $seconds, int $microseconds)
bool timed_out ()
int write (string $string, [int $length = null])
int write_chunk (string $string)
Variables
int $error_number (line 69)

Last error number

string $error_string (line 63)

Last error string

int $max_header_lines = 100 (line 75)

Maximum header lines

int $max_line_length = 0 (line 82)

Maximum header line length.

Setting this to 0 indicates no limit on the length

int $socket (line 57)

Socket (result of fsockopen())

Methods
Constructor HTTP_Connection (line 102)

Constructor

Example:

void HTTP_Connection (mixed $host, [mixed $port = 80], [mixed $timeout_connection = 10], [mixed $options = null], int $status_code, string $reason_phrase, object $headers, string $body)
  • int $status_code
  • string $reason_phrase
  • object $headers:

    headers object

  • string $body:

    message body

clear_error (line 385)

Clear errors

bool clear_error ()
configure (line 123)

Configure

bool configure (array $options)
  • array $options
disconnect (line 354)

Disconnect from server

bool disconnect ()
eof (line 401)

EOF

Tests for end-of-file. If a server keeps the connection open (e.g. if there's no "Connection: close" header in the response), eof() will return false even if you've read the whole response.

bool eof ()
format_chunk (line 153)

Format as chunk (support for reading chunked responses is required by HTTP/1.1)

string format_chunk (mixed $string)
get_error_number (line 144)

Get error number

int get_error_number ()
get_error_string (line 135)

Get error string

string get_error_string ()
get_unread_bytes (line 421)

Get unread bytes - the number of bytes currently contained in the read buffer.

int get_unread_bytes ()
has_error (line 376)

Has error

bool has_error ()
read (line 167)

Read

Like builtin fread(): reads up to length bytes. Reading stops when length bytes have been read or EOF (end of file) reached, whichever comes first.

string read (int $length)
  • int $length
read_chunk (line 212)

Read chunk

If a chunk is recognised, an array will be returned holding chunk data and the chunk size. Otherwise a string will be returned holding the result of read_line().

Example:


1 $chunk = $http->read_chunk();
2 if (is_array($chunk)) {
3 $chunk_found = true;
4 echo "Chunk size: ",$chunk[1];
5 echo "Chunk data: ",$chunk[0];
6 } else {
7 $found_chunk = false;
8 $content = $chunk.$http->read(1024);
9 }

mixed read_chunk ()
read_headers (line 261)

Read headers.

Headers will be returned in an array:

 [0] => (0 => 'Host', 1 => 'host.com')
 [1] => (0 => 'Connection', 1 => 'close')
 ...
 
Because there can be multiple headers with the same name, we don't put it into an associative array, if we did the previous header of the same name would be overwritten.

array read_headers ()
read_line (line 187)

Read line

Like builtin fgets(): Returns a string of up to length - 1 bytes. Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, the length defaults to 1k, or 1024 bytes.

  • return:

    false on error

string read_line ([mixed $length = 1024])
read_status_line (line 236)

Read status line

This method will ignore empty lines (lines with no content, or just white space), and will assume the first non-empty line encountered is the status line. If status line is matched, an array will be returned with the following keys: (0 => HTTP-Version, 1 => Status-Code, 2 => Reason-Phrase) If status line is not found, the first non-empty line encountered will be returned.

  • return:

    array if status line is found, string otherwise

mixed read_status_line ()
set_timeout (line 367)

Set read/write timeout

bool set_timeout (int $seconds, int $microseconds)
  • int $seconds:

    seconds

  • int $microseconds:

    microseconds (default: 0)

timed_out (line 410)

Timed out

bool timed_out ()
write (line 334)

Write

Like builtin fwrite(): writes the contents of string. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

  • return:

    number of bytes written, or false on error

int write (string $string, [int $length = null])
  • string $string
  • int $length:

    (optional)

write_chunk (line 345)

Write chunk

  • return:

    number of bytes written, or false on error

int write_chunk (string $string)
  • string $string

Documention generated on Wed, 16 Jul 2003 01:03:17 +0100 by phpDocumentor 1.2.0