PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » HTTP » HTTP2 » Manual

Provides a set of useful functions related to the Hyper-Text-Transfer-Protocol. PHP 5 compatible.

HTTP2::date

HTTP2::date – format a date

Synopsis

require_once 'HTTP2.php';

string HTTP::date ( integer time )

Description

Converts a UNIX timestamp into an RFC compliant HTTP header line. This function honors the "y2k_compliance" php.ini directive.

Parameter

Return value

string - a RFC compliant date header line

Note

This function can not be called statically.

HTTP2::head

HTTP2::head – sends a "HEAD" command

Synopsis

require_once 'HTTP2.php';

array HTTP2::head ( string $url )

Description

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

Example

HEAD request to example.com

<?php
require_once "PEAR.php";
require_once "HTTP2.php";

$http = new HTTP2();
try {
    $result = $http->head("http://example.com/");
    echo "<pre>";
    print_r($result);
    echo "</pre>";
} catch (HTTP2_Exception $e) {
    echo "Error: " . $e->getMessage();
}
?>

The output of the print_r() call is shown below.

Parameter

Return value

array - an array containing the header lines or a PEAR_Error.

Example output:

<?php
Array
(
    [response_code] => 200
    [response] => HTTP/1.1 200 OK
    [Date] => Tue, 25 Nov 2003 22:08:57 GMT
    [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
    [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
    [ETag] => "3f80f-1b6-3e1cb03b"
    [Accept-Ranges] => bytes
    [Content-Length] => 438
    [Connection] => close
    [Content-Type] => text/html
)
?>

Throws

An exception of type HTTP2_Exception is thrown when an error occurs.

Note

This function can not be called statically.

HTTP2::negotiateLanguage

HTTP2::negotiateLanguage – Negotiate language with the user's browser

Synopsis

require_once 'HTTP.php';

string HTTP2::negotiateLanguage ( array $supported , string $default = 'en_US' )

Description

Negotiate 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". Two-letter country codes can be found in the ISO 3166 standard.

Quantities in the Accept-Language: header are supported, for example:

Accept-Language: en-UK;q=0.7, en-US;q=0.6, no;q=1.0, dk;q=0.8

Example

Usage example

<?php
require_once "HTTP2.php";

$supported = array("de" => true, "en-US" => true);

$http = new HTTP2();
echo $http->negotiateLanguage($supported);
?>

This example negotiates with the user agent if any of the languages, which are specified in supported, are supported on the user's system. If the negotiation has a positive result, the language code of the most preferred language is printed. Otherwise the default language code (en-US) is printed.

Being able to perform language negotiation is a big help when developing internationalized website with pages, that are available in more than one language. Using negotiation, the user will always get pages in the language which he prefers. (Assuming that their user agent is configured properly.)

Parameter

Return value

string - a language code

Note

This function can not be called statically.

The returned language is only a hint! Sending the accepted languages by the client is optional. The language settings of the browser do not have to meet the user's native language - for example a german traveller in a spanish internet cafe. You can improve your result by combining it with the result of the Net_Geo package. Apart from that, you should still give the user the chance to manually choose their preferred language menu.

HTTP2::redirect

HTTP2::redirect – redirects the client

Synopsis

require_once 'HTTP2.php';

void HTTP2::redirect ( string url )

Description

This function redirects the client. This is done by issuing a Location: header and exiting.

Example

Redirecting to another site

<?php
require_once 'HTTP2.php';
$http = new HTTP2();
$http->redirect("http://example.com/");
?>

Local redirect

<?php
require_once 'HTTP.php';
$http = new HTTP2();
$http->redirect("/foo.php");
?>

This will redirect the client to /foo.php. The method will take care of adding the right hostname as required by RFC 2616.

Parameter

Note

This function can not be called statically.

Avoid sending any kind of data to the client before calling redirect().

The location header requires an absolute URL. If not given, redirect() tries to build one from $url. So if the redirect fails, set the absolute URL manually as argument.