PEAR Manual
Prev |  | Next

# Introduction

Introduction -- Introduction to HTTP_Request

## Overview

With this package, one can easily perform HTTP request from within PHP scripts. It support GET/POST/HEAD/TRACE/PUT/DELETE, Basic authentication, Proxy, Proxy Authentication, SSL, file uploads etc.

Because of the above mentioned features HTTP_Request makes it possible to mimic big parts of web browsers such as the widely-known Mozilla browser in PHP applications. Possible application areas are:

- Checking the validity of WWW links with the help of getResponseCode().

- Grabbing remote web pages and parsing the result.

- etc.

## A few examples

Example 42-1. Fetches yahoo.com and displays it
    require_once "HTTP/Request.php";

    $req =& new HTTP_Request("http://www.yahoo.com/");
    if (!PEAR::isError($req->sendRequest())) {
        echo $req->getResponseBody();
    }

Example 42-2. Fetching two website in a row In this example, two websites are fetched and displayed. To the first one a POST parameter is passed. The POST data stack is cleared before the second website is fetched.
    require_once "HTTP/Request.php";

    $req =& new HTTP_Request("http://www.php.net");
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addPostData("Foo", "bar");
    if (!PEAR::isError($req->sendRequest())) {
         $response1 = $req->getResponseBody();
    } else {
         $response1 = "";
    }

    $req->setMethod(HTTP_REQUEST_METHOD_GET);
    $req->setURL("http://pear.php.net");
    $req->clearPostData();
    if (!PEAR::isError($req->sendRequest())) {
         $response2 = $req->getResponseBody();
    } else {
         $response2 = "";
    }

    echo $response1;
    echo $response2;

Prev | Home | Next
HTTP_Request | Up | Basic Authentication