How can PEAR packages be effectively utilized in PHP to streamline the process of accessing password-protected web pages?

To streamline the process of accessing password-protected web pages in PHP, PEAR packages such as HTTP_Request2 can be utilized. This package allows for easy handling of HTTP requests, including sending authentication credentials to access password-protected pages.

<?php
require_once 'HTTP/Request2.php';

$request = new HTTP_Request2('http://example.com/protected-page.php');
$request->setAuth('username', 'password', HTTP_Request2::AUTH_BASIC);

try {
    $response = $request->send();
    if ($response->getStatus() == 200) {
        echo $response->getBody();
    } else {
        echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' . $response->getReasonPhrase();
    }
} catch (HTTP_Request2_Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
?>