PEAR is archived and read-only

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

Home » HTTP » HTTP » Manual

Provides a set of usefull, static functions related to the Hyper-Text-Transfer-Protocol. Warning: The HTTP package is deprecated. Use the HTTP2 package instead.

HTTP::date

HTTP::date – format a date

Synopsis

require_once 'HTTP.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.

The HTTP package is deprecated. Use HTTP2::date instead.

Parameter

Return value

string - a RFC compliant date header line

Note

This function can be called statically.

HTTP::head

HTTP::head – sends a "HEAD" command

Synopsis

require_once 'HTTP.php';

array HTTP::head ( string $url )

Description

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

The HTTP package is deprecated. Use HTTP2::head instead.

Example

HEAD request to example.com

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

$result = HTTP::head("http://example.com/");

if (PEAR::isError($result)) {
    echo "Error: " . $result->getMessage();
} else {
    echo "<pre>";
    print_r($result);
    echo "</pre>";
}
?>

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

Possible PEAR_Error values
Error code Error message Reason Solution
NULL "HTTP::head Error $errstr ($erno)" Connection to server failed Check connectivity of your host and the given URL in $url

Note

This function can be called statically.

HTTP::negotiateLanguage

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

Synopsis

require_once 'HTTP.php';

string HTTP::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

The HTTP package is deprecated. Use HTTP2::negotiateLanguage instead.

Example

Usage example

<?php
require_once "HTTP.php";

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

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

HTTP::redirect

HTTP::redirect – redirects the client

Synopsis

require_once 'HTTP.php';

void HTTP::redirect ( string url )

Description

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

The HTTP package is deprecated. Use HTTP2::redirect instead.

Example

Redirecting to another site

<?php
require_once 'HTTP.php';

HTTP::redirect("http://example.com/");
?>

Local redirect

<?php
require_once 'HTTP.php';

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