9.2. Zend_Http_Response

9.2.1. Introduction

Zend_Http_Response provides easy access to the responses returned by Section 9.1, “Zend_Http_Client”. It provides an intuitive set of methods for working with the HTTP response data received from a request:

  • isError(): Returns TRUE if an HTTP error code was received; FALSE otherwise.
  • isSuccessful(): Returns TRUE if an HTTP success code was received; FALSE otherwise.
  • isRedirect(): Returns TRUE if an HTTP redirect code was received; FALSE otherwise.
  • getStatus(): Returns the HTTP status code.
  • getHeaders(): Returns an array of HTTP response header strings.
  • getBody(): Returns the HTTP response body as a string.

Example 9.6. Working with HTTP Response Data

<?php
require_once 'Zend/Http/Client.php';
try {
    $http = new Zend_Http_Client('http://example.org');
    $response = $http->get();
    if ($response->isSuccessful()) {
        echo $response->getBody();
    } else {
        echo "<p>An error occurred</p>\n";
        echo "HTTP Status: " . $response->getStatus() . "\n";
        echo "HTTP Headers:\n";
        $responseHeaders = $response->getHeaders();
        foreach ($responseHeaders as $responseHeaderName => $responseHeaderValue) {
            echo "$responseHeaderName: $responseHeaderValue\n";
        }
    }
} catch (Zend_Http_Client_Exception $e) {
    echo '<p>An error occurred (' .$e->getMessage(). ')</p>';
}
?>