\PlufHTTP2

Miscellaneous HTTP Utilities

PEAR::HTTP2 provides shorthand methods for generating HTTP dates, issueing HTTP HEAD requests, building absolute URIs, firing redirects and negotiating user preferred language.

Summary

Methods
Properties
Constants
date()
negotiateLanguage()
negotiateCharset()
negotiateMimeType()
head()
redirect()
absoluteURI()
parseLinks()
No public properties found
No constants found
matchAccept()
sortAccept()
convertCharset()
No protected properties found
N/A
No private methods found
No private properties found
N/A

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, boolean  $exit = true, boolean  $rfc2616 = false) : boolean

This function redirects the client.

This is done by issuing a "Location" header and exiting if wanted. If you set $rfc2616 to true HTTP will output a hypertext note with the location of the redirect.

Parameters

string $url

URL where the redirect should go to.

boolean $exit

Whether to exit immediately after redirection.

boolean $rfc2616

Wheter to output a hypertext note where we're redirecting to (Redirecting to ....)

Returns

boolean —

Returns TRUE on succes (or exits) or FALSE if headers have already been sent.

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".

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