PEAR Manual
Prev |  | Next

# Response Evaluation

Response Evaluation -- Evaluating the information from a HTTP response

## Introduction

Because HTTP is a protocol based on the Request - Response scheme, every HTTP request is followed by a HTTP response. HTTP_Request offers several methods to evaluate the information from these responses.

## Response Codes

A important part of the HTTP response is the response code. The most well-known response code probably is `404`, which you may have seen in your browser at several occasions. The meaning of `404` is that the requested ressource could not be found. A complete list of status codes can be found in RFC 2616.

Example 42-1. Checking the response code
    require_once "HTTP/Request.php";

    $urls = array(
        "http://www.example.com/",
        "http://example.com/thisdoesnotexist.html"
        );

    $req =& new HTTP_Request("");
    foreach ($urls as $url) {
        $req->setURL($url);
        $req->sendRequest();

        $code = $req->getResponseCode();
        switch ($code) {
        case 404:
            echo "Document not found\n";
            break;

        case 200:
            echo "Everything's ok\n";
            break;

        /* ... */
        }
    }

## Response Headers

Similar to a HTTP request a HTTP response consists of a header and a body. HTTP_Request offers a method to access the header of the response.

Example 42-2. Getting all headers from the response
    require_once "HTTP/Request.php";

    $req =& new HTTP_Request("http://example.com/");
    $req->sendRequest();

    foreach ($req->getResponseHeader() as $name => $value) {
        echo $name . " = " . $value . "\n";
    }
This will print all headers and the appendant values.

Example 42-3. Getting a specific header
    require_once "HTTP/Request.php";

    $req =& new HTTP_Request("http://example.com/");
    $req->sendRequest();

    echo $req->getResponseHeader("Date");
This will print the value of the `Date:` header.

## Response Cookies

Fetching the cookies that are part of the HTTP response is described in the Cookies section.

Prev | Home | Next
Proxy Authorization | Up | HTTP_Upload