Constants

STATUS_100

STATUS_100 = '100 Continue'

STATUS_101

STATUS_101 = '101 Switching Protocols'

STATUS_102

STATUS_102 = '102 Processing'

STATUS_INFORMATIONAL

STATUS_INFORMATIONAL = 1

STATUS_200

STATUS_200 = '200 OK'

STATUS_201

STATUS_201 = '201 Created'

STATUS_202

STATUS_202 = '202 Accepted'

STATUS_203

STATUS_203 = '203 Non-Authoritative Information'

STATUS_204

STATUS_204 = '204 No Content'

STATUS_205

STATUS_205 = '205 Reset Content'

STATUS_206

STATUS_206 = '206 Partial Content'

STATUS_207

STATUS_207 = '207 Multi-Status'

STATUS_SUCCESSFUL

STATUS_SUCCESSFUL = 2

STATUS_300

STATUS_300 = '300 Multiple Choices'

STATUS_301

STATUS_301 = '301 Moved Permanently'

STATUS_302

STATUS_302 = '302 Found'

STATUS_303

STATUS_303 = '303 See Other'

STATUS_304

STATUS_304 = '304 Not Modified'

STATUS_305

STATUS_305 = '305 Use Proxy'

STATUS_306

STATUS_306 = '306 (Unused)'

STATUS_307

STATUS_307 = '307 Temporary Redirect'

STATUS_REDIRECT

STATUS_REDIRECT = 3

STATUS_400

STATUS_400 = '400 Bad Request'

STATUS_401

STATUS_401 = '401 Unauthorized'

STATUS_402

STATUS_402 = '402 Payment Granted'

STATUS_403

STATUS_403 = '403 Forbidden'

STATUS_404

STATUS_404 = '404 File Not Found'

STATUS_405

STATUS_405 = '405 Method Not Allowed'

STATUS_406

STATUS_406 = '406 Not Acceptable'

STATUS_407

STATUS_407 = '407 Proxy Authentication Required'

STATUS_408

STATUS_408 = '408 Request Time-out'

STATUS_409

STATUS_409 = '409 Conflict'

STATUS_410

STATUS_410 = '410 Gone'

STATUS_411

STATUS_411 = '411 Length Required'

STATUS_412

STATUS_412 = '412 Precondition Failed'

STATUS_413

STATUS_413 = '413 Request Entity Too Large'

STATUS_414

STATUS_414 = '414 Request-URI Too Large'

STATUS_415

STATUS_415 = '415 Unsupported Media Type'

STATUS_416

STATUS_416 = '416 Requested range not satisfiable'

STATUS_417

STATUS_417 = '417 Expectation Failed'

STATUS_422

STATUS_422 = '422 Unprocessable Entity'

STATUS_423

STATUS_423 = '423 Locked'

STATUS_424

STATUS_424 = '424 Failed Dependency'

STATUS_CLIENT_ERROR

STATUS_CLIENT_ERROR = 4

STATUS_500

STATUS_500 = '500 Internal Server Error'

STATUS_501

STATUS_501 = '501 Not Implemented'

STATUS_502

STATUS_502 = '502 Bad Gateway'

STATUS_503

STATUS_503 = '503 Service Unavailable'

STATUS_504

STATUS_504 = '504 Gateway Time-out'

STATUS_505

STATUS_505 = '505 HTTP Version not supported'

STATUS_507

STATUS_507 = '507 Insufficient Storage'

STATUS_SERVER_ERROR

STATUS_SERVER_ERROR = 5

Properties

$prettify

$prettify : boolean

Type

boolean

$_headers

$_headers : array

Default Headers

The values that are set as default, are the same as PHP sends by default.

Type

array

$_httpVersion

$_httpVersion : string

HTTP version

Type

string

Methods

date()

date(mixed  $time = null) : string|boolean

Formats a RFC compliant GMT date HTTP header.

This function honors the "y2k_compliance" php.ini directive and formats the GMT date corresponding to either RFC850 or RFC822.

Parameters

mixed $time

unix timestamp or date (default = current time)

Returns

string|boolean —

GMT date string, or FALSE for an invalid $time parameter

negotiateLanguage()

negotiateLanguage(array  $supported, string  $default = 'en-US') : string

Negotiates language with the user's browser through the Accept-Language HTTP header or the user's host address.

Language codes are generally in the form "ll" for a language spoken in only one country, or "ll-CC" for a language spoken in a particular country. For example, U.S. English is "en-US", while British English is "en-UK". Portugese as spoken in Portugal is "pt-PT", while Brazilian Portugese is "pt-BR".

Quality factors in the Accept-Language: header are supported, e.g.: Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8

require_once 'HTTP2.php'; $http = new HTTP2(); $langs = array( 'en' => 'locales/en', 'en-US' => 'locales/en', 'en-UK' => 'locales/en', 'de' => 'locales/de', 'de-DE' => 'locales/de', 'de-AT' => 'locales/de', ); $neg = $http->negotiateLanguage($langs); $dir = $langs[$neg];

Parameters

array $supported

An associative array of supported languages, whose values must evaluate to true.

string $default

The default language to use if none is found.

Returns

string —

The negotiated language result or the supplied default.

negotiateCharset()

negotiateCharset(array  $supported, string  $default = 'ISO-8859-1') : string

Negotiates charset with the user's browser through the Accept-Charset HTTP header.

Quality factors in the Accept-Charset: header are supported, e.g.: Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8

require_once 'HTTP2.php'; $http = new HTTP2(); $charsets = array( 'UTF-8', 'ISO-8859-1', ); $charset = $http->negotiateCharset($charsets);

Parameters

array $supported

An array of supported charsets

string $default

The default charset to use if none is found.

Returns

string —

The negotiated language result or the supplied default.

negotiateMimeType()

negotiateMimeType(array  $supported, string  $default) : string

Negotiates content type with the user's browser through the Accept HTTP header.

Quality factors in the Accept: header are supported, e.g.: Accept: application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8

require_once 'HTTP2.php'; $http = new HTTP2(); $contentType = array( 'application/xhtml+xml', 'application/xml', 'text/html', 'text/plain', ); $mime = $http->negotiateMimeType($contentType);

Parameters

array $supported

An associative array of supported MIME types.

string $default

The default type to use if none match.

Returns

string —

The negotiated MIME type result or the supplied default.

head()

head(string  $url, integer  $timeout = 10) : array

Sends a "HEAD" HTTP command to a server and returns the headers as an associative array.

Example output could be: Array ( [response_code] => 200 // The HTTP response code [response] => HTTP/1.1 200 OK // The full HTTP response string [Date] => Fri, 11 Jan 2002 01:41:44 GMT [Server] => Apache/1.3.20 (Unix) PHP/4.1.1 [X-Powered-By] => PHP/4.1.1 [Connection] => close [Content-Type] => text/html )

Parameters

string $url

A valid URL, e.g.: http://pear.php.net/credits.php

integer $timeout

Timeout in seconds (default = 10)

Throws

\Pluf\HTTP2\Exception

When connecting fails

\InvalidArgumentException

When the protocol is not uspported

Returns

array —

Returns associative array of response headers on success

redirect()

redirect(string  $url = null, array  $param = array(), boolean  $session = false) : void

Redirect

This function redirects the client. This is done by issuing a Location header and exiting. Additionally to HTTP2::redirect() you can also add parameters to the url.

If you dont need parameters to be added, simply use HTTP2::redirect() otherwise use Header2::redirect().

Parameters

string $url

The URL to redirect to, if none is given it redirects to the current page.

array $param

Array of query string parameters to add; usually a set of key => value pairs; if an array entry consists only of an value it is used as key and the respective value is fetched from $GLOBALS[$value]

boolean $session

Whether the session name/id should be added

absoluteURI()

absoluteURI(string  $url = null, string  $protocol = null, integer  $port = null) : string

This function returns the absolute URI for the partial URL passed.

The current scheme (HTTP/HTTPS), host server, port, current script location are used if necessary to resolve any relative URLs.

Offsets potentially created by PATH_INFO are taken care of to resolve relative URLs to the current script.

You can choose a new protocol while resolving the URI. This is particularly useful when redirecting a web browser using relative URIs and to switch from HTTP to HTTPS, or vice-versa, at the same time.

Parameters

string $url

Absolute or relative URI the redirect should go to.

string $protocol

Protocol to use when redirecting URIs.

integer $port

A new port number.

Returns

string —

The absolute URI.

parseLinks()

parseLinks(string|array  $lines) : array

Parses a HTTP "Link" header value as specified by RFC 5988.

When errors occur during parsing, all information collected to that point will be returned used and the rest of the line skipped.

Parameters

string|array $lines

HTTP "Link:" header value (without "Link:"), or array of such strings

Returns

array —

Array of parsed links (one single line may contain multiple links) Only params existing in the link are in the array. URI is available in key "_uri".

__construct()

__construct() : object

Constructor

Sets HTTP version.

Returns

object —

Header2

setHttpVersion()

setHttpVersion(mixed  $version) : boolean

Set HTTP version

Parameters

mixed $version

HTTP version, either 1.0 or 1.1

Returns

boolean —

Returns true on success or false if version doesn't match 1.0 or 1.1 (note: 1 will result in 1.0)

getHttpVersion()

getHttpVersion() : string

Get HTTP version

Returns

string

setHeader()

setHeader(string  $key, string  $value = null) : boolean

Set Header

The default value for the Last-Modified header will be current date and time if $value is omitted.

Parameters

string $key

The name of the header.

string $value

The value of the header. (NULL to unset header)

Returns

boolean —

Returns true on success or false if $key was empty or $value was not of an scalar type.

getHeader()

getHeader(string  $key = null) : mixed

Get Header

If $key is omitted, all stored headers will be returned.

Parameters

string $key

The name of the header to fetch.

Returns

mixed —

Returns string value of the requested header, array values of all headers or false if header $key is not set.

sendHeaders()

sendHeaders(array  $keys = array(), array  $include = true) : boolean

Send Headers

Send out the header that you set via setHeader().

Parameters

array $keys

Headers to (not) send, see $include.

array $include

If true only $keys matching headers will be sent, if false only header not matching $keys will be sent.

Returns

boolean —

Returns true on success or false if headers are already sent.

sendStatusCode()

sendStatusCode(integer  $code) : boolean

Send Satus Code

Send out the given HTTP-Status code. Use this for example when you want to tell the client this page is cached, then you would call sendStatusCode(304). \Pluf\HTTP\Header2_Cache::exitIfCached()

Parameters

integer $code

The status code to send, i.e. 404, 304, 200, etc.

Returns

boolean —

Returns true on success or false if headers are already sent.

dateToTimestamp()

dateToTimestamp(string  $date) : mixed

Date to Timestamp

Converts dates like Mon, 31 Mar 2003 15:26:34 GMT Tue, 15 Nov 1994 12:45:26 GMT into a timestamp, strtotime() didn't do it in older versions.

Parameters

string $date

The GMT date.

Returns

mixed —

Returns int unix timestamp or false if the date doesn't seem to be a valid GMT date.

getStatusType()

getStatusType(string  $http_code) : integer|false

Return HTTP Status Code Type

Parameters

string $http_code

HTTP status code

Returns

integer|false

getStatusText()

getStatusText(string  $http_code) : string|false

Return Status Code Message

Parameters

string $http_code

HTTP status code

Returns

string|false

isInformational()

isInformational(string  $http_code) : boolean

Checks if HTTP Status code is Information (1xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

isSuccessful()

isSuccessful(string  $http_code) : boolean

Checks if HTTP Status code is Successful (2xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

isRedirect()

isRedirect(string  $http_code) : boolean

Checks if HTTP Status code is a Redirect (3xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

isClientError()

isClientError(string  $http_code) : boolean

Checks if HTTP Status code is a Client Error (4xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

isServerError()

isServerError(string  $http_code) : boolean

Checks if HTTP Status code is Server Error (5xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

isError()

isError(string  $http_code) : boolean

Checks if HTTP Status code is Server OR Client Error (4xx or 5xx)

Parameters

string $http_code

HTTP status code

Returns

boolean

matchAccept()

matchAccept(string  $header, array  $supported) : string|NULL

Parses a weighed "Accept" HTTP header and matches it against a list of supported options

Parameters

string $header

The HTTP "Accept" header to parse

array $supported

A list of supported values

Returns

string|NULL —

a matched option, or NULL if no match

sortAccept()

sortAccept(string  $header) : array

Parses and sorts a weighed "Accept" HTTP header

Parameters

string $header

The HTTP "Accept" header to parse

Returns

array —

Sorted list of "accept" options

convertCharset()

convertCharset(string  $from, string  $to, string  $str) : string

Convert the given string from one into another charset.

Uses mb_convert_encoding or iconv if available.

Parameters

string $from

Source charset the string is in

string $to

Target character set

string $str

String to convert

Returns

string —

converted string